selenium2.0报错:stale element reference: element is not attached to the page document的解决办法

使用selenium webdriver编写自动化脚本时,遇到异常报错为:
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

经各方查证,大概意思是说:意思就是Element已经过时,没有和当前页面进行绑定,主要是页面刷新或者跳转引起的,需要重新获取一次元素。

修改之前代码如下:

action.find_element_by_css_selector("div[node-id='93']").click()
        time.sleep(2)
        action.switch_to.frame("93")        
        sreach_code=snapshot+date
        time.sleep(2)
        action.find_element_by_xpath("//*[@id='container']/div/ul/li[2]/a/span").click()
        time.sleep(2)       
        list_data=action.find_elements_by_css_selector("#tab_todo>div>div.row.approvals>a")     
        for ld in list_data:
            if snapshot in str(ld.text.encode("utf-8")):
                if date in str(ld.text.encode("utf-8")):
                    ld.click()

修改之后代码如下:

action.find_element_by_css_selector("div[node-id='93']").click()
        time.sleep(2)
        action.switch_to.frame("93")        
        sreach_code=snapshot+date
        time.sleep(2)
        action.find_element_by_xpath("//*[@id='container']/div/ul/li[2]/a/span").click()
        time.sleep(2)       
        list_data=action.find_elements_by_css_selector("#tab_todo>div>div.row.approvals>a")     
        for ld in list_data:
            if snapshot in str(ld.text.encode("utf-8")):
                if date in str(ld.text.encode("utf-8")):
                    subscript=list_data.index(ld)
        list_data[subscript].click()

没有再次报错的原因不明,仅此记录,待后续查证。

参考资料:
WebUI自动化测试问题总结—基于Dagger http://zhangzhenzhen2009.blog.163.com/blog/static/187039247201481871342610/

你可能感兴趣的:(selenium2.0报错:stale element reference: element is not attached to the page document的解决办法)