4

So I'm starting to get the hang of while loops, however when using the while loop on a selenium code, I come up short.

Pretty much I'm trying to replicate a task 10 times, here is what the code looks like

Main.py

from selenium import webdriver
from selenium.webdriver.common.keys import Keys


driver = webdriver.Chrome()
driver.get('https://orlando.craigslist.org/search/cta')

owl = driver.find_element_by_xpath('//*[@id="sortable-results"]/ul/li/p/a')
res = 1

while res < 10:
    owl2 = owl.click()
    driver.find_element_by_xpath('/html/body/section/header/nav/ul/li[3]/p/a').click()

    res = res + 1

here is the error

Traceback (most recent call last): File "main.py", line 12, in owl2 = owl.click() File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 77, in click self._execute(Command.CLICK_ELEMENT) 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)

Any Suggestions ?

3 Answers 3

6

Every time the DOM is changing or refreshing the driver losses the elements it previously located witch cause the error.

StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

You need to relocate them in order to interact with them. In addition, click() doesn't returns any value so you can't assign it to anything

res = 1
while res < 10:
    owl = driver.find_element_by_xpath('//*[@id="sortable-results"]/ul/li/p/a')
    owl.click()
    driver.find_element_by_xpath('/html/body/section/header/nav/ul/li[3]/p/a').click()
    res = res + 1

Edit

With for loop for all the items you can locate the items into a list and click by index

size = len(driver.find_elements_by_xpath('//*[@id="sortable-results"]/ul/li/p/a'))
for i in range(0, size):
    owl = driver.find_elements_by_xpath('//*[@id="sortable-results"]/ul/li/p/a')
    owl[i].click()
    driver.find_element_by_xpath('/html/body/section/header/nav/ul/li[3]/p/a').click()
Sign up to request clarification or add additional context in comments.

3 Comments

this works great, now you don't have to answer this but, how would you do this in a "For Loop". for each listing
@OwlMan I added an example of for loop
@OwlMan Edited to include all the items.
2

The error message gives us a clue:

Message: stale element reference: element is not attached to the page document

What's happening here is that you are clicking on a link, and navigating to another page, hence, you get stale element because you're on a different page. You will want to navigate back to the same page, try something like this:

driver.execute_script("window.history.go(-1)")

after the click() event.

1 Comment

still doesn't replicate the task 10 times though
0

Very simply said: you must start your test case with loop, means "for" must be the first command in your test case and everything afterthat. Example:

def test_case(self):
     login_page = LoginPage(self.driver)
     for element in element_to_check:
          assert element

won't work. It stops after first cycle. Instead just rewrite your code like this:

def test_case(self):
 for element in element_to_check:
      login_page = LoginPage(self.driver)
      assert element

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.