python3.6+selenium+phantomJS 网页爬虫报错NoSuchElementException问题及解决方法 - Go语言中文社区

python3.6+selenium+phantomJS 网页爬虫报错NoSuchElementException问题及解决方法


错误信息:

selenium.common.exceptions.NoSuchElementException: Message: {"errorMessage":"Unable to find element with xpath '//*[@id='**']'","request":{"headers":{"Accept":"application/json",...}}
Screenshot: available via screen
由于代码换用Chrome浏览器是正常执行的,所以个人认为原因是由于使用phantomJS后,动态JS还没有解析,没有获取到网页代码,所以才会报NoSuchElementException

所以写一个方法等JS解析就行了得意

代码如下:

# JS
def wait(driver):
    elem = driver.find_element_by_tag_name('html')
    count = 0
    while True:
        count +=1
        if count>20:
            print('timeout')
            return
        time.sleep(5)
        try:
            elem == driver.find_element_by_tag_name('html')
        except StaleElementReferenceException:
            return
在报错的语句前使用wait(driver)

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/ariel_dy/article/details/78730718
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-08 11:21:21
  • 阅读 ( 476 )
  • 分类:前端

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢