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

来源:互联网 发布:java短信验证码第三方 编辑:程序博客网 时间:2024/04/30 07:28

错误信息:

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解析就行了得意

代码如下:

# JSdef 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)

阅读全文
0 0
原创粉丝点击