StaleElementReferenceException Selenium WebDriver Python

韦达瓦

我正在使用Selenium,Python和PhantomJS编写爬虫程序,以使用Google的反向图像搜索。到目前为止,我已经能够成功上传图像并在第一页上爬网搜索结果。但是,当我尝试单击搜索结果导航时,出现了StaleElementReferenceError。我在很多帖子中都读过它,但是仍然无法实现该解决方案。这是中断的代码:

ele7 = browser.find_element_by_id("nav")
ele5 = ele7.find_elements_by_class_name("fl")

count = 0
for elem in ele5:
    if count <= 2:
        print str(elem.get_attribute("href"))
        elem.click()
        browser.implicitly_wait(20)
        ele6 = browser.find_elements_by_class_name("rc")
        for result in ele6:
            f = result.find_elements_by_class_name("r")
            for line in f:
                link = line.find_elements_by_tag_name("a")[0].get_attribute("href")
                links.append(link)
                parsed_uri = urlparse(link)
                domains.append('{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri))
            count += 1 

代码在打印时中断str(elem.get_attribute("href"))我该如何解决?

提前致谢。

虚假的

单击一个链接将使浏览器转到另一个页面。使在旧页面(该元素的引用ele5elem)无效。

修改代码以不引用无效的元素。

例如,您可以在访问其他页面之前获取URL:

ele7 = browser.find_element_by_id("nav")
ele5 = ele7.find_elements_by_class_name("fl")

urls = [elem.get_attribute('href') for elem in ele5]  # <-----

browser.implicitly_wait(20)

for url in urls[:2]:  # <------
    print url
    browser.get(url)  # <------ used `browser.get` instead of `click`.
                      #         ; using `element.click` will cause the error.

    ele6 = browser.find_elements_by_class_name("rc")
    for result in ele6:
        f = result.find_elements_by_class_name("r")
        for line in f:
            link = line.find_elements_by_tag_name("a")[0].get_attribute("href")
            links.append(link)
            parsed_uri = urlparse(link)
            domains.append('{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri))

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python Selenium Webdriver:调试

来自分类Dev

使用Python和Selenium Webdriver抓取JavaScript

来自分类Dev

使用Selenium Webdriver(Python)循环链接

来自分类Dev

存储图像的标题文本Selenium Webdriver Python

来自分类Dev

Python Selenium Webdriver-尝试除循环

来自分类Dev

使用Selenium WebDriver作为基类python

来自分类Dev

无法在Python中将Firefox WebDriver与Selenium连接

来自分类Dev

Selenium Webdriver Python,无法按值查找?

来自分类Dev

Selenium WebDriver Python,搜索WebElement

来自分类Dev

Scrapy和Selenium StaleElementReferenceException

来自分类Dev

python selenium selenium.common.exceptions.StaleElementReferenceException错误

来自分类Dev

Python Selenium上的StaleElementReferenceException错误

来自分类Dev

Python Selenium Webdriver-代理:查询参数

来自分类Dev

无法单击此按钮Selenium Webdriver Python

来自分类Dev

使用Selenium Webdriver,Python检索动态值

来自分类Dev

Python-Pyinstaller-导入Selenium WebDriver

来自分类Dev

Webdriver不适用于Selenium(Python)

来自分类Dev

python硒中的StaleElementReferenceException

来自分类Dev

Python Selenium Webdriver click()无法正常工作

来自分类Dev

Python Selenium:处理Webdriver异常

来自分类Dev

如何避免python selenium中的StaleElementReferenceException错误

来自分类Dev

Selenium fluentWait导致StaleElementReferenceException

来自分类Dev

Selenium Webdriver Python storeTextPresent

来自分类Dev

Selenium Python StaleElementReferenceException

来自分类Dev

Selenium + Python 上的 StaleElementReferenceException

来自分类Dev

Selenium Webdriver 获取元素(Python)

来自分类Dev

使用 Python Selenium 获取“selenium.common.exceptions.StaleElementReferenceException”错误

来自分类Dev

Python Selenium:StaleElementReferenceException / 设置 aria-pressed = "true"

来自分类Dev

Python Selenium webdriver超时异常

Related 相关文章

  1. 1

    Python Selenium Webdriver:调试

  2. 2

    使用Python和Selenium Webdriver抓取JavaScript

  3. 3

    使用Selenium Webdriver(Python)循环链接

  4. 4

    存储图像的标题文本Selenium Webdriver Python

  5. 5

    Python Selenium Webdriver-尝试除循环

  6. 6

    使用Selenium WebDriver作为基类python

  7. 7

    无法在Python中将Firefox WebDriver与Selenium连接

  8. 8

    Selenium Webdriver Python,无法按值查找?

  9. 9

    Selenium WebDriver Python,搜索WebElement

  10. 10

    Scrapy和Selenium StaleElementReferenceException

  11. 11

    python selenium selenium.common.exceptions.StaleElementReferenceException错误

  12. 12

    Python Selenium上的StaleElementReferenceException错误

  13. 13

    Python Selenium Webdriver-代理:查询参数

  14. 14

    无法单击此按钮Selenium Webdriver Python

  15. 15

    使用Selenium Webdriver,Python检索动态值

  16. 16

    Python-Pyinstaller-导入Selenium WebDriver

  17. 17

    Webdriver不适用于Selenium(Python)

  18. 18

    python硒中的StaleElementReferenceException

  19. 19

    Python Selenium Webdriver click()无法正常工作

  20. 20

    Python Selenium:处理Webdriver异常

  21. 21

    如何避免python selenium中的StaleElementReferenceException错误

  22. 22

    Selenium fluentWait导致StaleElementReferenceException

  23. 23

    Selenium Webdriver Python storeTextPresent

  24. 24

    Selenium Python StaleElementReferenceException

  25. 25

    Selenium + Python 上的 StaleElementReferenceException

  26. 26

    Selenium Webdriver 获取元素(Python)

  27. 27

    使用 Python Selenium 获取“selenium.common.exceptions.StaleElementReferenceException”错误

  28. 28

    Python Selenium:StaleElementReferenceException / 设置 aria-pressed = "true"

  29. 29

    Python Selenium webdriver超时异常

热门标签

归档