Using python and selenium, I load a page with firefox driver. a list of links are displayed, I can collect those links, but when I attempt to click on each one in turn (which advances to a page with data that I scrape) and then return to the previous page with the list of links and click the next one, the Stale Element Exception is thrown. Have watched more than 10 videos, read more than 20 posts on supposedly how to solve this, multiple options tried have not worked.
Here is my code.
links = driver.find_elements_by_partial_link_text('2019 TRC')
for link in links:
link.click()
get_data() #this gathers data on the new page appearing after clicking the link
driver.back() #this travels back to page with link list, but then fails with stale element exception
My intent is to gather the links into a list, iterate through the list clicking on each link (which opens a new page with data) gather that data, return to the page with the links, click the next link until the link list is exhausted.
I have tried all of these options, all failed.
#1
# for case in links:
# try:
# case.click()
# except StaleElementReferenceException:
# links = driver.find_elements_by_xpath("//*[contains(text(), '2019 TRC')]")
# case.click()
# driver.back()
#2
# for case in links:
# try:
# case.click()
# except StaleElementReferenceException:
# driver.find_elements_by_xpath("//*[contains(text(), '2019 TRC')]")
# case.click()
# driver.back()
#3 - This just keeps going back after returning to case list page
# for case in links:
# try:
# case.click()
# except StaleElementReferenceException:
# driver.find_elements_by_xpath("//*[contains(text(), '2019 TRC')]")
# time.sleep(1)
# driver.back()
#4 Stays at the link list page
# for case in links:
# try:
# case.click()
# except StaleElementReferenceException:
# driver.find_elements_by_xpath("//*[contains(text(), '2019 TRC')]")
# case.click()
# time.sleep(1)
# driver.back()
#5 stays at the link list page
# for case in links:
# try:
# case.click()
# except StaleElementReferenceException:
# links = driver.find_elements_by_xpath("//*[contains(text(), '2019 TRC')]")
# case.click()
# time.sleep(1)
# driver.back()
#6 Changed elements to element (single) and this stays as the link list page again
# for case in links:
# try:
# case.click()
# except StaleElementReferenceException:
# driver.find_element_by_xpath("//*[contains(text(), '2019 TRC')]")
# case.click()
# time.sleep(1)
# driver.back()
#7 Stayed on the first case detail page
#
# for case in links:
# try:
# case.click()
# except StaleElementReferenceException:
# driver.find_element_by_xpath("//*[contains(text(), '2019 TRC')]")
# case.click()
# driver.back()
#8 DId not work using links= instead of just the driver.
# for case in links:
# try:
# case.click()
# except StaleElementReferenceException:
# links = driver.find_element_by_xpath("//*[contains(text(), '2019 TRC')]")
# case.click()
# driver.back()
I even tried finding the links with "by_partial_link_text" or "by_id" and still, same error surfaces.
If you quickly mark this question as already answered, please note that I have read the other posts on Stackoverflow, just now and none of them offered a solution which worked. I tried approximately five of them.