2

I'm trying to go on a webpage, save a set of links of the page I would like to click on, and then I would like to click on each of the link if a for loop (going back and forth on the page. Here is the code:

from selenium import webdriver


driver = webdriver.Chrome(executable_path='/Applications/chromedriver')
driver.get("webpage link") #insert link to webpage
list_links = driver.find_elements_by_xpath("//a[contains(@href,'activities')]")


for link in list_links:
    print(link)
    link.click()
    driver.goback()
    driver.implicitly_wait(10) # seconds
driver.quit()

However, the first time I go back to the homepage I get the error message:

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

Can anyone help me to understand why? Suggest a solution? Thank you. much appreciated.

4
  • Its simple, you trying to save the elements of an html(links) which cannot be referenced anymore by the code(the loop logic), thats why it throws this error. Most of all those are selenium objects you trying to save which you should not do. It should be that you save the exact value of the link in an array and then loop them. Commented Nov 16, 2017 at 11:47
  • thanks Hassan. Have you got any suggestions to fix this? Commented Nov 16, 2017 at 11:48
  • Just save the link(exact hyper link value) in an array to use them later in the loop. Commented Nov 16, 2017 at 11:49
  • @Reti43, my bad ! Ill post it :) Commented Nov 16, 2017 at 11:54

2 Answers 2

3

Your list_links works only on page where it was defined. After you made first click on link DOM re-created and references to elements of list_links becomes invalid. You can apply below solution:

driver.implicitly_wait(10) # seconds
list_links = [link.get_attribute('href') for link in driver.find_elements_by_xpath("//a[contains(@href,'activities')]")]

for link in list_links:
    print(link)
    driver.get(link)
    driver.goback()
driver.quit()

P.S. I assume that goback() is your custom method that was defined already as there is no such method in Selenium built-ins, but just back()

P.P.S. Note that you can call driver.implicitly_wait(10) only once in your code and it will be applicable for all next find_element...() calls

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

3 Comments

Thanks very much Andersson. I understood the mistake. Also, I believe that get(link) will click on the link and in conjunction with back() it will allow me to go back and forth. I'll do some more reading on this. But thanks very much!!
Not exactly. get(link) just allows you to navigate to the same URL as if you click on link. And back() navigates you back to previous page
Thanks. Appreciated!
0

Its simple, you trying to save the elements of an html(links) which cannot be referenced anymore by the code(the loop logic), thats why it throws this error. Most of all those are selenium objects you trying to save which you should not do. It should be that you save the exact value of the link in an array and then loop them.

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.