Don#39;t wait for a page to load using Selenium in Python(不要等待在 Python 中使用 Selenium 加载页面)
问题描述
如何让 selenium 在页面完全加载之前点击元素并抓取数据?我的互联网连接非常糟糕,所以有时需要很长时间才能完全加载页面,这有什么问题吗?
How do I make selenium click on elements and scrape data before the page has fully loaded? My internet connection is quite terrible so it sometimes takes forever to load the page entirely, is there anyway around this?
推荐答案
ChromeDriver 77.0(支持 Chrome 77 版)现在支持 eager
作为 pageLoadStrategy.
ChromeDriver 77.0 (which supports Chrome version 77) now supports eager
as pageLoadStrategy.
已解决的问题 1902:支持急切页面加载策略 [Pri-2]
Resolved issue 1902: Support eager page load strategy [Pri-2]
<小时>
当你提到在页面完全加载之前点击元素并抓取数据
在这种情况下,我们可以利用属性pageLoadStrategy
强>.当 Selenium 默认加载页面/url 时,它遵循默认配置,将 pageLoadStrategy
设置为 normal
.Selenium 可以从不同的文档就绪状态
开始执行下一行代码.目前 Selenium 支持 3 种不同的 Document readiness state
,我们可以通过 pageLoadStrategy
配置如下:
As you question mentions of click on elements and scrape data before the page has fully loaded
in this case we can take help of an attribute pageLoadStrategy
. When Selenium loads a page/url by default it follows a default configuration with pageLoadStrategy
set to normal
. Selenium can start executing the next line of code from different Document readiness state
. Currently Selenium supports 3 different Document readiness state
which we can configure through the pageLoadStrategy
as follows:
无
(未定义)eager
(页面变为交互式)正常
(完成页面加载)
这是配置pageLoadStrategy
的代码块:
Here is the code block to configure the pageLoadStrategy
:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
binary = r'C:Program FilesMozilla Firefoxfirefox.exe'
caps = DesiredCapabilities().FIREFOX
# caps["pageLoadStrategy"] = "normal" # complete
caps["pageLoadStrategy"] = "eager" # interactive
# caps["pageLoadStrategy"] = "none" # undefined
driver = webdriver.Firefox(capabilities=caps, firefox_binary=binary, executable_path="C:\Utility\BrowserDrivers\geckodriver.exe")
driver.get("https://google.com")
这篇关于不要等待在 Python 中使用 Selenium 加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:不要等待在 Python 中使用 Selenium 加载页面


基础教程推荐
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01