How to click links one by one with Selenium webdriver and Python


Site has top menu with 6 links. I can get list of this links like this:

links = browser.find_elements_by_css_selector(MENU_LINKS_CSS_SELECTOR)

After this I need to click this links one by one. If I do it like this:

for link in links:

I get the following error: selenium.common.exceptions.StaleElementReferenceException: Message: u'Element not found in the cache - perhaps the page has changed since it was looked up'. As I understand, this error raises beacause of connection betweeb WebElement instances and DOM of the web-page is broken after reloading the page (clicking on link).

Here I should notice that top menu is the same on all pages.

So, what I do wrong? How to fix this? TIA!


I don’t know much Selenium but you should select the links again –

for i in range(0,6):
    links = browser.find_elements_by_css_selector(MENU_LINKS_CSS_SELECTOR)

Answered By – svineet

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published