2

im trying to print out the title, for the the links that is programmed to click on. I am having some troubles. However i get not an error but something else. Yes, i have tried "text" but that actually gives me an error

Main.py

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
import unittest

class Test():
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://orlando.craigslist.org/search/apa?s=100")

    def task(self):
        driver = self.driver
        ln = driver.find_elements_by_xpath('//*[@id="sortable-results"]/ul/li/p/a')
        size = len(driver.find_elements_by_xpath('//*[@id="sortable-results"]/ul/li/p/a'))

        for i in range(0, size):
            ln = driver.find_elements_by_xpath('//*[@id="sortable-results"]/ul/li/p/a')
            ln = ln[i]
            ln.click()
            print (ln)

            back = driver.find_element_by_xpath('/html/body/section/header/nav/ul/li[3]/p/a').click()
            WebDriverWait(driver,3)
            if i == 5:
                break

    def getInfo(self,ln):
        driver = self.driver
        if ln:
            print (ln)
        else:
            print ("No listing name")


    def initialize():
        return Test

    def teardown(self):
        self.driver.quit()



run = Test()
run.setUp()
tas = run.task()
run.teardown()
WebDriverWait(tas,3)





if __name__ == '__main__':
    unittest.main()

here is what i get

<selenium.webdriver.remote.webelement.WebElement (session="214c8aa4e4a919e11149b48fc1971ebf", element="0.47651721823993864-1")>
<selenium.webdriver.remote.webelement.WebElement (session="214c8aa4e4a919e11149b48fc1971ebf", element="0.8722193647053464-2")>
<selenium.webdriver.remote.webelement.WebElement (session="214c8aa4e4a919e11149b48fc1971ebf", element="0.7501031429429339-3")>
<selenium.webdriver.remote.webelement.WebElement (session="214c8aa4e4a919e11149b48fc1971ebf", element="0.1697348713718274-4")>
<selenium.webdriver.remote.webelement.WebElement (session="214c8aa4e4a919e11149b48fc1971ebf", element="0.8560861666106063-5")>
<selenium.webdriver.remote.webelement.WebElement (session="214c8aa4e4a919e11149b48fc1971ebf", element="0.8604981647081367-6")>

here is what print(ln.text) gets me

Traceback (most recent call last):
  File "main.py", line 45, in <module>
    tas = run.task()
  File "main.py", line 20, in task
    print (ln.text)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 73, in text
    return self._execute(Command.GET_ELEMENT_TEXT)['value']
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 491, in _execute
    return self._parent.execute(command, params)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 238, in execute
    self.error_handler.check_response(response)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 193, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=56.0.2924.87)
  (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Mac OS X 10.11.2 x86_64)
Community
  • 1
  • 1
BARNOWL
  • 3,326
  • 10
  • 44
  • 80

1 Answers1

4

What you see is the WebElement instance representation string. Instead, you need to get the text of an element by getting the value of the .text attribute:

print(ln.text)

Also note that WebDriverWait(driver, 3) line would actually do "nothing" (well, except making an instance of WebDriverWait that would be soon garbage-collected), if you want a hardcoded time delay, use time.sleep(3), though a better idea would be to explicitly wait for a certain condition to be met via WebDriverWait and .until().

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195