What does Selenium .set_script_timeout(n) do and how is it different from driver.set_page_load_timeout(n)?(Selenium .set_script_timeout(n) 有什么作用,它与 driver.set_page_load_timeout(n) 有何不同?)
问题描述
在 python selenium 的上下文中,我不太了解 driver.set_page_load_timeout(n)
VS 的确切区别.driver.set_script_timeout(n)
.两者似乎可以互换使用来设置超时以通过 driver.get(URL)
加载 URL,但有时也可以一起使用.
In context of python selenium, I don't quite understand the exact difference of driver.set_page_load_timeout(n)
VS. driver.set_script_timeout(n)
. Both seem to be used interchangeable to set a timeout to load an URL via driver.get(URL)
, but sometimes also together.
场景 1:
driver.set_page_load_timeout(5)
website = driver.get(URL)
results = do_magic(driver, URL)
场景 2:
driver.set_script_timeout(5)
website = driver.get(URL)
results = do_magic(driver, URL)
这两种情况有何不同?哪些情况会在一种情况下触发超时,而在另一种情况下不会触发?
推荐答案
根据 Selenium-Python API Docs set_page_load_timeout(n)
和 set_script_timeout(n)
都是 timeout 方法,用于配置 webdriver 实例在程序执行期间遵守.
As per the Selenium-Python API Docs set_page_load_timeout(n)
and set_script_timeout(n)
both are timeout methods which are used to configure the webdriver instance to abide by during the program execution.
set_page_load_timeout(time_to_wait)
设置在抛出错误之前等待页面加载完成的时间量,定义为:
set_page_load_timeout(time_to_wait)
sets the amount of time to wait for a page load to complete before throwing an error and is defined as :
def set_page_load_timeout(self, time_to_wait):
"""
Set the amount of time to wait for a page load to complete
before throwing an error.
:Args:
- time_to_wait: The amount of time to wait
:Usage:
driver.set_page_load_timeout(30)
"""
try:
self.execute(Command.SET_TIMEOUTS, {
'pageLoad': int(float(time_to_wait) * 1000)})
except WebDriverException:
self.execute(Command.SET_TIMEOUTS, {
'ms': float(time_to_wait) * 1000,
'type': 'page load'})
在这里您可以找到关于 set_page_load_timeout
Here you can find a detailed discussion on set_page_load_timeout
set_script_timeout(time_to_wait)
设置脚本在 execute_async_script
(Javascript/AJAX Call) 在抛出错误之前调用,定义为:
set_script_timeout(time_to_wait)
sets the amount of time that the script should wait during an execute_async_script
(Javascript / AJAX Call) call before throwing an error and is defined as :
def set_script_timeout(self, time_to_wait):
"""
Set the amount of time that the script should wait during an
execute_async_script call before throwing an error.
:Args:
- time_to_wait: The amount of time to wait (in seconds)
:Usage:
driver.set_script_timeout(30)
"""
if self.w3c:
self.execute(Command.SET_TIMEOUTS, {
'script': int(float(time_to_wait) * 1000)})
else:
self.execute(Command.SET_SCRIPT_TIMEOUT, {
'ms': float(time_to_wait) * 1000})
这篇关于Selenium .set_script_timeout(n) 有什么作用,它与 driver.set_page_load_timeout(n) 有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Selenium .set_script_timeout(n) 有什么作用,它与 driver.set_page_load_timeout(n) 有何不同?
基础教程推荐
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01