Bootstrap

记录selenium报错element is not attached to the page document解决方案

这个案例不是因为等待时间不够的原因,是在调用writecontent这个方法之后,原来的页面会刷新,导致了元素过期,再次点击的时候就会报错。

原代码:

elmt=self.driver.find_elements_by_xpath("//*[text()='去填写']")
for ele in elmt:
    ele.click()
    self.writecontent()
    time.sleep(2)

 

解决方案:

    elmt=self.driver.find_elements_by_xpath("//*[text()='去填写']")
    num=len(elmt)
    #做个循环
    for i in range(num):
        #循环里面重新去定位元素
        elmt = self.driver.find_elements_by_xpath("//*[text()='去填写']")
        #因为我每次都只需要点击第一个去填写所以索引写的0
        elmt[0].click()
        self.writecontent()
        time.sleep(2)
;