I navigate to a page and then find the list of episodes. I get each episode and click on the link for the episode. But when I go back to the page that has the list of episodes the following error happens:
stale element reference: element is not attached to the page document
My code is:
navigator.get('https://anchor.fm/dashboard/episodes')
time.sleep(5)
#get list
list_episodes = navigator.find_element_by_xpath('//*[@id="app-content"]/div/div/div/div[2]/ul')
#get episodes in list
items = list_episodes.find_elements_by_tag_name('li')
for item in items:
item.find_element_by_tag_name('button').click()
time.sleep(10)
navigator.find_element_by_xpath('//*[@id="app-content"]/div/div/div/div[2]/div/div[2]/div/div/div/button').click()
time.sleep(2)
navigator.find_element_by_xpath('//*[@id="app-content"]/div/div/div/div[2]/div/div[2]/div/div/div/div/div/div[1]/button[6]').click()
time.sleep(2)
navigator.find_element_by_xpath('//*[@id="app-content"]/div/div/div/div[3]/div[1]/div/div/div/button').click()
time.sleep(2)
navigator.find_element_by_xpath('//*[@id="app-content"]/div/div/div/div[3]/div[1]/div/div/div/div/div/div/button[1]').click()
time.sleep(2)
navigator.find_element_by_xpath('//*[@id="app-content"]/div/div/div/div[3]/div[3]/div/div/a').click()
time.sleep(3)
navigator.get('https://anchor.fm/dashboard/episodes')
time.sleep(5)
CodePudding user response:
By navigating to another page all collected by selenium web elements (they are actually references to a physical web elements) become no more valid since the web page is re-built when you open it again.
To make your code working you need to collect the items
list again each time.
This should work:
navigator.get('https://anchor.fm/dashboard/episodes')
time.sleep(5)
#get list
list_episodes = navigator.find_element_by_xpath('//*[@id="app-content"]/div/div/div/div[2]/ul')
#get episodes in list
items = list_episodes.find_elements_by_tag_name('li')
for i in range(len(items)):
item = items[i]
item.find_element_by_tag_name('button').click()
time.sleep(10)
navigator.find_element_by_xpath('//*[@id="app-content"]/div/div/div/div[2]/div/div[2]/div/div/div/button').click()
time.sleep(2)
navigator.find_element_by_xpath('//*[@id="app-content"]/div/div/div/div[2]/div/div[2]/div/div/div/div/div/div[1]/button[6]').click()
time.sleep(2)
navigator.find_element_by_xpath('//*[@id="app-content"]/div/div/div/div[3]/div[1]/div/div/div/button').click()
time.sleep(2)
navigator.find_element_by_xpath('//*[@id="app-content"]/div/div/div/div[3]/div[1]/div/div/div/div/div/div/button[1]').click()
time.sleep(2)
navigator.find_element_by_xpath('//*[@id="app-content"]/div/div/div/div[3]/div[3]/div/div/a').click()
time.sleep(3)
navigator.get('https://anchor.fm/dashboard/episodes')
time.sleep(5)
#get the `items` list again
items = list_episodes.find_elements_by_tag_name('li')
CodePudding user response:
In this case, it's helpful to think of the pages as instances of a class. They may have the same name, the same properties, the same values but they're still separate objects and you can't call object A if you have a reference to object B.
Here's what's happening to you in this case; I highlighted the interesting parts.
- You navigate to a directory page
2. Server builds an instance of the page & displays it to you
- You get a hold of episode objects on the page
- You navigate to one of the episodes
5. Server destroys the directory page. Any objects in it you were
holding disappear with it
6. Server builds a copy of the episode page & displays it to you
- You navigate back to the directory page
8. Server builds a new instance of the page & displays it to you
- You try to click an element from the old instance of the page that no longer exists
- You get a stale reference exception because, well - your reference is now stale
The way to fix this is to find the episode elements each time you navigate to the directory page. If you find them once and store them, they'll go bad as soon as you navigate elsewhere and their parent page poofs.
Also, a note about your Xpaths: I'd encourage you to stop using your browser's 'Copy Xpath' function, it doesn't often get good results. There are plenty of tutorials on how to write good Xpaths online that are worth reading.