不要在 Python 中使用 Selenium 等待页面加载 [英] Don't wait for a page to load using Selenium in Python

查看:54
本文介绍了不要在 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种不同的文档准备状态,我们可以通过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:

  1. none(未定义)
  2. eager(页面变成交互式)
  3. normal(完整的页面加载)

这是配置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 等待页面加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆