前言
selenium定位一组元素,批量操作循环点击的时候会报错:element is not attached to the page document
实现目标:批量点击标题,获取每个页面的url地址
代码如下:
# coding:utf-8
from selenium import webdriver
driver=webdriver.Chrome()
driver.get("https://www.cnblogs.com/canglongdao/")
titles=driver.find_elements_by_xpath("//div[@id='main']/div/h2/a")
for i in titles:
i.click()
print(driver.current_url)
driver.back()
运行结果:
https://www.cnblogs.com/canglongdao/p/13613712.html
Traceback (most recent call last):
File "E:/study/selenium_study/a825.py", line 7, in
i.click()
...
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page docu