Python Selenium WebDriver如何为get(url)函数添加超时

Aar*_*ker 8 python selenium timeout webdriver driver

我正在运行一段通过代理下载网站的简单代码,但有时代理可能很慢,这可能会导致WebDriver .get(url)请求无限期地阻塞.

WebDriver是否有一段简单的Python代码可以为此函数设置超时?通过搜索我只发现了适用于java的技术.

driver.get(url)
Run Code Online (Sandbox Code Playgroud)

Aar*_*ker 7

对于那里的所有网站,我过去常常解决这个问题.Selenium使用套接字库,所以我在套接字模块上设置了一个超时,这会抛出一个错误,我们可以用它将转义键发送到浏览器(停止页面加载):

socket.setdefaulttimeout(2)
try:
     driver.get(pageLink)
except socket.timeout:
     #send ESCAPE key to browser
Run Code Online (Sandbox Code Playgroud)


小智 4

在文档中找到了这个

selenium.webdriver.remote.webdriver.set_script_timeout(time_to_wait)

Set the amount of time that the script should wait before throwing an error.

time_to_wait: The amount of time to wait

Usage:

driver.set_script_timeout(30)
Run Code Online (Sandbox Code Playgroud)