记一次selenium + Chrom 无法捕获的异常 报错 selenium.common.exceptions.TimeoutException: Message: timeout
最新推荐文章于 2023-03-01 13:05:50 发布
qq_41928442
于 2020-07-14 11:02:08 发布
10564
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/WenShuSpider_v1/DetailCrawl/ChromeProxyDetail_4.py", line 86, in get_detail
self.driver.get(url)
File "C:\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 333, in get
self.execute(Command.GET, {'url': url})
File "C:\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: timeout
(Session info: chrome=73.0.3683.103)
(Driver info: chromedriver=70.0.3538.97 (d035916fe243477005bc95fe2a5778b8f20b6ae1),platform=Windows NT 10.0.17134 x86_64)
抓取某网站的时候,由于网站经常会响应比较慢,或者还没加载完全就返回空白页,导致程序经常检测不到元素卡死,返回
selenium.common.exceptions.TimeoutException: Message: timeout
通过在 try ... except 后面添加 finally: 当try和except没有完成时让页面刷新程序重新调用(刷新页面或许有用)
def get_detail(self, url):
msgtimeout = 1
print('访问网址:{}'.format(url))
self.driver.get(url)
myxpath = '//div[@class="PDF_pox"]//*[text()]'
locator = (By.XPATH, myxpath)
self.wait.until(EC.presence_of_element_located(locator))
time.sleep(0.25)
html = self.driver.page_source
msgtimeout = 0
return html
except Exception as e:
print('获取详情页失败:{}'.format(e))
# 清除 cookie
self.driver.delete_all_cookies()
print('清除cookie 重新访问 {}'.format(url))
msgtimeout = 0
return self.get_detail(url)
finally:
if msgtimeout:
print('wo 来处理timeout异常')
return self.get_detail(url)
记一次selenium + Chrom 无法捕获的异常 报错 selenium.common.exceptions.TimeoutException: Message: timeout
Traceback (most recent call last): File "C:/Users/Administrator/Desktop/WenShuSpider_v1/DetailCrawl/ChromeProxyDetail_4.py", line 86, in get_detail self.driver.get(url) File "C:\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.
比如我们有大量ip,去访问某个页面,但我们并不知道ip的稳定性,如果一个ip
异常
不能使用,可能会导致整个代码停掉,后面
无法
测试。
这时候就要用
selenium
自带的
异常
来解决:
# -*-encoding: utf-8-*...
raise
exception
_class(
message
, screen, stacktrace)
selenium
.
common
.
exception
s.ElementNotInteractable
成功解决:
selenium
.
common
.
exception
s.
Timeout
Exception
:
Message
:
timeout
: Timed out receiving
message
from renderer: 294.905
(Session info: headless
chrome
=102.0.5005.115)
现在网络越来越普及,带宽速度普遍也有较大提升。伴随网络的普及,同时快速增长的还有网民的数量和网络流量。网站超时响应的“通病”,仍然是
无法
完全避免的。尤其是在网络流量高峰期,在使用类似
Selenium
的
浏览器
内核驱动driver方案过程中,这类问题的处理是关键。下面以
Selenium
WebDriver和Firefox为例子,介绍如何多层次应对网站超时响应的问题。
初始化设置WebDriver的页面加载和脚本执行超时限制。
from
selenium
import webdriver
from sele
selenium
.
common
.
exception
s.
Timeout
Exception
:
Message
: 问题解决
好几天没学基础知识了,开始写自动化的框架了,中间遇到的问题,解决之后都
记
录下~
写了一个find_elements文件,调试的时候一执行
报错
:
D:\my\
python
\
python
.exe D:/my/learning/kuangjia/ZTKXin/Utilpackage/find_elements.py
****** xpath id
Traceback (most recent
异常
:
selenium
.
common
.
exception
s.ElementNotSelectable
Exception
(msg=None, screen=None, stacktrace=None)
依据:
selenium
.
common
.
exception
s.InvalidElementState
Exception
当尝试选择一个未能选中的元素时,
异常
会抛出
selenium
.
common
.
exception
s.WebDriver
Exception
:
Message
: '
chrome
driver’解决:
https://blog.csdn.net/weixin_44318830/article/details/103339273
今天在做
selenium
测试的时候,可能是很久没用了,直接报了这个
异常
!
相信很多第
一次
学习
selenium
的同学们也对这个
异常
不陌生了,
from appium import webdriver
from
selenium
.
common
.
exception
s import NoSuchElement
Exception
from time import sleep
from
selenium
.webdriver.support.ui import WebDriverWait
#调用TouchAction
from appium.web...
-
selenium
.
common
.
exception
s.WebDriver
Exception
:
Message
: unknown error: cannot find
Chrome
binary
报错
问题:已配置环境变量,并把
chrome
driver配置到
Python
目录下,运行文件后
报错
操作:修改环境变量配置,还是
报错
浏览器
与驱动的版本不适配,
浏览器
版本
chrome
://version/查看,为86.0.4240.111 (正式版本) (64 位),而驱动用了现成的是71的(据说兼容性比