【发布时间】:2017-03-22 22:44:28
【问题描述】:
我制作新脚本,我想点击()网站上所有列出的链接,找到一些东西,回到列出的链接,click()下一个链接,找到一些东西,回到列出的链接。
我从为我列出一些链接的网站开始:
链接 1 链接 2 链接 3 等等
links = driver.find_elements_by_xpath("myxpath")
for link in links:
link.click()
try:
time.sleep(2)
wantedelement = driver.find_element_by_xpath("xpath")
wantedelement.click()
#Save to file
tofile = driver.find_element_by_xpath("xpath")
print (tofile.text)
myfile = open("file.txt", "a")
my.write(tofile.text + "\n")
driver.back()
except (ElementNotVisibleException, NoSuchElementException):
driver.back()
但我的脚本只检查一个链接,当返回列出的链接时会打印错误:
selenium.common.exceptions.StaleElementReferenceException:消息:元素引用已过时。元素不再附加到 DOM 或页面已刷新。
on line:
for link in links:
link.click() <----
我该如何解决? (Python 2.7)
【问题讨论】:
标签: python python-2.7 selenium selenium-webdriver