简体   繁体   中英

Second iterative of find_elements_by_xpath gives error in selenium python

I'm trying to find all the my subject in my dashboard of my college website. I'm using selenium to do it. The site is a little slow so first I wait

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='multiline']")))

then I find all the elements with

course = driver.find_elements_by_xpath("//span[@class='multiline']")

after that in a for loop I try to traverse it the 0th place of the "course" works fine and I'm able to click it and go to webpage but when the loop runs for the secon d time that is for the 1st place in "course" it gives me error selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

So I tried adding a lit bit wait time to using 2 method it still gives me error

driver.implicitly_wait(20)


WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='multiline']")))

the loop

for i in course[1::]:

#driver.implicitly_wait(20)

#WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='multiline']")))

print(i)
i.click()

driver.implicitly_wait(2)

driver.back()

a snippet of the website

在此处输入图像描述

Thanks in advance

Answering my own question after extensive research

A common technique used for simulating a tabbed UI in a web app is to prepare DIVs for each tab, but only attach one at a time, storing the rest in variables. In this case my code have a reference to an element that is no longer attached to the DOM (that is, that has an ancestor which is "document.documentElement").

If WebDriver throws a stale element exception in this case, even though the element still exists, the reference is lost. You should discard the current reference you hold and replace it, possibly by locating the element again once it is attached to the DOM

for i in range(len(course)):

    # here you need to find all the elements again because once we
     leave the page the reference will be lost and we need to find it again
    course = driver.find_elements_by_xpath("//span[@class='multiline']")

    print(course[i].text)
    course[i].click()

    driver.implicitly_wait(2)

    driver.back()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM