【问题标题】:How to interact with an element before it is rendered within the HTML DOM through Selenium and Python如何在元素通过 Selenium 和 Python 在 HTML DOM 中呈现之前与元素交互
【发布时间】:2018-07-18 23:29:31
【问题描述】:

是否可以在页面呈现之前请求一个 URL 并检查元素? 我正在使用 Python + Selenium。

【问题讨论】:

  • 为什么要在页面呈现之前检查元素?你的目标是什么?你有一些code trials 供我们处理吗?
  • 我正在查看日历中的可用日期以进行预约。没有理由加载整个页面,只需检查元素即可。

标签: python-3.x selenium selenium-webdriver webdriver pageloadstrategy


【解决方案1】:

对您的问题的一个字回答将是

说明

一般来说,网页上的每个 WebElement 都有 3(三)种不同的状态,如下所示:

  • presence:元素存在于页面的 DOM 中。
  • visibility:元素存在于页面的 DOM 中并且可见。
  • interactable(即clickable:元素可见且已启用,因此您可以单击它。

当 Selenium 默认加载网页/url 时,它遵循默认配置 pageLoadStrategy 设置为 normal。您可以选择不等待完整的页面加载。因此,为了避免等待整个网页加载,您可以配置pageLoadStrategypageLoadStrategy 支持如下 3 种不同的值:

  1. normal(完成页面加载)
  2. eager(互动)
  3. none

这是一个配置pageLoadStrategy的示例代码块:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

caps = DesiredCapabilities().FIREFOX.copy()
caps["pageLoadStrategy"] = "none"
driver = webdriver.Firefox(desired_capabilities=caps, executable_path=r'C:\path\to\geckodriver.exe')
driver.get("http://google.com")

您可以在How to make Selenium not wait till full page load, which has a slow script?找到详细讨论

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-01
    • 2023-03-09
    • 2021-09-15
    • 1970-01-01
    • 2020-11-17
    • 2020-03-23
    相关资源
    最近更新 更多