Python StaleElementReferenceException:消息:陈旧元素引用:元素未附加到页面文档

时间:2019-05-13 07:10:35

标签: python python-3.x selenium selenium-webdriver

我是硒中的新手,当我在for循环中完成工作时,出现了错误。第一次循环在第二次达到以下错误时效果很好。

[1, 1, 0]
[1, 0, 0]
[1, 0, 1]

我尝试使用Time.wait以及webdriver.wait

elenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

只想在每次for循环工作中使用此代码 它的工作是第一次循环时的工作,第二次是给定的错误。

1 个答案:

答案 0 :(得分:0)

单击将导航到另一页,这意味着驱动程序丢失了先前定位的元素。您需要在每次迭代时重新定位它们。您可以使用索引来跟踪列表中的位置

def function(foo):
    driver.get("https://truckbhada.com/PostedLoadDetails?st=Maharashtra")
    token = driver.find_elements_by_xpath('//table[@class="k-selectable"]/tbody/tr/td/a')

    for i in range(len(token)):
        tr = driver.find_elements_by_xpath('//table[@class="k-selectable"]/tbody/tr/td/a')
        tr[i].click()
        tr = driver.find_element_by_id("ContentPlaceHolder1_btnloaddetails")
        tr.click()
        print("---------")
        print(driver.find_element_by_id("ContentPlaceHolder1_lblmobile").text)
        print("---=====---")
        driver.back()
        driver.back()