0

all_divs contain all the links i got . I need to click all of them but when i try to do , selenum throws stale error saying

"selenium.common.exceptions.StaleElementReferenceException: Message: The element reference of 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 "

link_div = driver.find_elements_by_css_selector("div[id='sidebar1269']")[0]


        all_divs  = link_div.find_elements_by_css_selector("div>a:last-of-type")

        print(all_divs[0])

        for i in all_divs:
            i.click()
Sudip Adhikari
  • 157
  • 2
  • 12

1 Answers1

0

Without knowing the html to possibly give a more efficient answer, this should get you what you want. After clicking a link, the dom is most likely updated/changed so you will need to re-find the elements before clicking.

link_div = driver.find_elements_by_css_selector("div[id='sidebar1269']")[0]

number_of_divs  = len(link_div.find_elements_by_css_selector("div>a:last-of-type"))

for i in range(number_of_divs):
    link_div = driver.find_elements_by_css_selector("div[id='sidebar1269']")[0]
    link_div.find_elements_by_css_selector("div>a:last-of-type")[i].click()
RKelley
  • 1,099
  • 8
  • 14