繁体   English   中英

单击带有Selenium Webdriver Python的一个一个链接

[英]Click links one by one with Selenium webdriver Python

我制作了脚本,该脚本搜索我感兴趣的字符串集,但是我有错误。 我如何解决以下问题:

links = driver.find_elements_by_xpath("xpath")

for x in range(0, len(links)):
    driver.implicitly_wait(2)
    links[x].click()
    try:
        driver.implicitly_wait(3)
        DO something
        driver.back()
        print("Mission completed!!")
    except (ElementNotVisibleException, NoSuchElementException):
        driver.back()
        print("No action")

Error:
selenium.common.exceptions.StaleElementReferenceException: Message: The element reference is stale. Either the element is no longer attached to the DOM or the page has been refreshed.
in line: links[x].click()

从列表links重定向到新的页面元素后,陈旧的links将无法使用。

您可以改用以下代码:

links = [link.get_attribute('href') for link in driver.find_elements_by_xpath("xpath")]
for link in links:
    driver.get(link)
    # DO something

这应该允许您获取参考列表并循环获取每个页面

单击链接有什么作用? 如果单击后导航到另一个页面,则迭代中的下一个页面不再位于DOM中,实际上已成为过时的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM