1

I make script which search for me intersted set of strings, but i have error. How i can resolve below problem:

links = driver.find_elements_by_xpath("xpath")

for x in range(0, len(links)):
    driver.implicitly_wait(2)
    links[x].click()
    try:
        driver.implicitly_wait(3)
        DO something
        driver.back()
        print("Mission completed!!")
    except (ElementNotVisibleException, NoSuchElementException):
        driver.back()
        print("No action")

Error:
selenium.common.exceptions.StaleElementReferenceException: Message: The element reference is stale. Either the element is no longer attached to the DOM or the page has been refreshed.
in line: links[x].click()
0

2 Answers 2

4

Once you re-directed to new page elements from your list links become stale- you cannot use them anymore.

You can use below code instead:

links = [link.get_attribute('href') for link in driver.find_elements_by_xpath("xpath")]
for link in links:
    driver.get(link)
    # DO something

This should allow you to get list of references and get each page in a loop

Sign up to request clarification or add additional context in comments.

2 Comments

thx man! You teach me a lot piece of good code :) This is the best wat to do this? My website look the same like google after search with 20 links.
If there is only one page with 20 links you can use it as it is, but if you have pagination (multiple pages with 20 links on each), then you might do another loop clicking Next button to append links from each page and then go through provided loop. One loop- to gather links, one- to follow links from list of references
1

What does clicking on the links do? If you navigate to another page after clicking it, then the next one in the iteration is no longer in the DOM, effectively becoming stale.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.