selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
for I in 抓取一组对象:
i.click()
报错:selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
因为点击页面后,for循环过来时候,原有抓取的对象已经失效了,需要重新获取。
或者抓取的对象放在数组里,点击对象后,数组里的对象已经失效了,需要再获取一次
可参考如下代码:
hot_city_list=dr.find_element_by_class_name("city-wrapper").find_elements_by_tag_name("a")
for i in range(0,17,1):
hot_city_list = dr.find_element_by_class_name("city-wrapper").find_elements_by_tag_name("a")#注意这句
hot_city_list[i+2].click()
print(hot_city_list[i+2].text)
time.sleep(1)