Reputation: 97
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()
Upvotes: 1
Views: 3357
Reputation: 52665
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
Upvotes: 4
Reputation: 36
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.
Upvotes: 1