python3.6+selenium+phantomJS 网页爬虫报错NoSuchElementException问题及解决方法
2017-12-06 15:19
736 查看
错误信息:
由于代码换用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)
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)
相关文章推荐
- python爬虫,selenium使用,Firefox-chrome-IE问题解决方法,selenium的基本操作
- 爬虫 解决网页ip限制的问题的八种方法
- Python 网络爬虫 006 (编程) 解决下载(或叫:爬取)到的网页乱码问题
- python selenium运行,打包exe常见问题解决方法
- 爬虫:selenium + phantomjs 解决js抓取问题(一)
- python3 request 爬虫 httplib.IncompleteRead() 问题的简单解决方法
- Python-selenium-Firefox-chrome-IE问题解决方法
- python3写爬虫程序时,遇到的问题及解决方法
- 对于Centos7,安装python3.6无法使用方向键问题的解决方法
- 【Python3.6爬虫学习记录】(七)使用Selenium+ChromeDriver爬取知乎某问题的回答
- 解决Python网页爬虫之中文乱码问题
- Python爬虫,用第三方库解决下载网页中文本的问题
- python中requests爬去网页内容出现乱码问题解决方法介绍
- Python爬虫学习——使用selenium和phantomjs爬取js动态加载的网页
- python爬虫解决网页重定向问题
- Python 网络爬虫 006 (编程) 解决下载(或叫:爬取)到的网页乱码问题
- python爬虫中文网页cmd打印出错问题解决
- 使用python+selenium制作浏览器爬虫,彻底解决ajax异步加载问题(待更新)
- [读书笔记]python爬虫-scrapy安装过程常见问题及解决方法
- 在Ubuntu14.04和Python3.4-3.6环境中,matplotlib不显示图像的问题的解决方法