Hi I try to iterate over elements, which is a button, that refers to a new page. After obtaining the relevant information within in the new page, I want to go back to the list of buttons (sometimes 2 pages back) and click on the next button. So think in a way of a graph tree, where I need to access every node.
So this is my structure:
url = 'www.firstpage.com'
driver.get(url)
Organizations = WebDriverWait(driver, 10).until(EC.visibility_of_any_elements_located((By.XPATH, "//button")))
for org in Organizations:
org.click()
Documents = WebDriverWait(driver, 10).until(EC.visibility_of_any_elements_located((By.XPATH, "//button")))
for document in Documents:
document.click()
##Do some stuff
......
##After finishing the requested operations, go back to the first page.
driver.get(url)
time.sleep(10) #To prevent a possible racing condition
org.click()
time.sleep(10) #To prevent a possible racing condition
When I do this, I get
selenium.common.exceptions.StaleElementReferenceException: Message: The element reference of
<button id="1343" > is
stale; either the element is no longer attached to the DOM, it is not in the current frame
context, or the document has been refreshed
It has probabily to do with refreshing the page, but I dont know how to solve this. The first loop works, but the moment I want to go to the second document, it gives the error.
I tried driver.back()
, but that definitely wont work since the structure of some pages (organizations) can differ (sometimes I need 2 times driver.back()
).
I tried to store the href
links, but those are not given in the elements. So that didn't work either.
Can someone help me to solve this.
CodePudding user response:
Basics of page clicking and using driver.back()
Say I have an element I need to loop which goes to another page from page click. I normally get the count of all elements and then just use the index. You can also (//button)[i] as an XPATH for a single element.
elems=driver.find_elements(By.XPATH,"//button")
for i in range(len(elems)):
driver.find_elements(By.XPATH,"//button")[i].click()
elems2=driver.find_elements(By.XPATH,"//button")
for j in range(len(elems2)):
driver.find_elements(By.XPATH,"//button")[j].click()
# To go back 1 page.
driver.back()