selenium遇到的一些问题,持续更新

1.今天早上运行程序的时候,发现我在循环点击一个元素的时候出现了错误

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=67.0.3396.79)
  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 6.1.7601 SP1 x86_64)

 大概的意思就是这个页面的元素引用已经过期了,在当前网页中找不到这个元素,我的代码大概是这个样子。

article_result_list=self.wait.until(EC.presence_of_all_elements_located((By.XPATH,"//div[@class='js_article_list']//a")))
                          for i in article_result_list:
                              url=i.get_attribute("href")
                             

 解决方法:去循环个数或者定位方式,在循环中获取元素。

length = len(driver.find_elements_by_tag_name("a")
for i in range(0,length):
    links = driver.find_elements_by_tag_name("a")
    link = links[i]
    link.click()

  

原文地址:https://www.cnblogs.com/c-x-a/p/9196986.html