Chrome 驱动的页面加载策略(更新到 Selenium v​​3.12.0) [英] Page load strategy for Chrome driver (Updated till Selenium v3.12.0)

查看:20
本文介绍了Chrome 驱动的页面加载策略(更新到 Selenium v​​3.12.0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Chrome 浏览器来测试 WebApp.

I'm using Chrome browser for testing WebApp.

有时页面会在很长一段时间后加载.我需要停止下载或限制他们的下载时间.

Sometimes pages loaded after very long time. I needed to stop downloading or limit their download time.

在 FireFox 中,我知道 PAGE_LOAD_STRATEGY = "eager".

In FireFox I know about PAGE_LOAD_STRATEGY = "eager".

chrome有类似的东西吗?

Is there something similar for chrome?

P.S.:driver.manage().timeouts().pageLoadTimeout() 有效,但之后对 Webdriver 的任何处理都会抛出 TimeOutException.停止启动后,我需要获取页面的当前 url.

P.S.: driver.manage().timeouts().pageLoadTimeout() works, but after that any treatment to Webdriver throws TimeOutException. I need to get the current url of the page after stopping its boot.

推荐答案

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]

<小时>

来自 Webdriver 规范:


From the Webdriver specs:

对于导致新文档加载的命令,命令返回的点由会话的页面加载策略决定.

Page Loading 花费太多时间并且您需要停止下载其他子资源(图像、css、js 等)时,您可以更改 pageLoadStrategy 通过 webdriver.

When Page Loading takes too much time and you need to stop downloading additional subresources (images, css, js etc) you can change the pageLoadStrategy through the webdriver.

在撰写本文时,pageLoadStrategy 支持以下值:

As of this writing, pageLoadStrategy supports the following values :

  1. 正常

这种策略导致 Selenium 等待整个页面加载(下载和解析 html 内容和子资源).

This stategy causes Selenium to wait for the full page loading (html content and subresources downloaded and parsed).

渴望

这种策略导致 Selenium 等待 DOMContentLoaded 事件(仅下载和解析 html 内容).

This stategy causes Selenium to wait for the DOMContentLoaded event (html content downloaded and parsed only).

此策略导致 Selenium 在完全接收到初始页面内容(已下载 html 内容)后立即返回.

This strategy causes Selenium to return immediately after the initial page content is fully received (html content downloaded).

默认情况下,当Selenium加载页面时,它遵循normal pageLoadStrategy.

By default, when Selenium loads a page, it follows the normal pageLoadStrategy.

这里是配置的代码块 pageLoadStrategy() 通过 DesiredCapabilities 类和 ChromeOptions 类的实例如下::

Here is the code block to configure pageLoadStrategy() through both an instance of DesiredCapabilities Class and ChromeOptions Class as follows : :

  • 使用 DesiredCapabilities 类:

package demo; //replace by your own package name

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

public class A_Chrome_DCap_Options {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe");
        DesiredCapabilities dcap = new DesiredCapabilities();
        dcap.setCapability("pageLoadStrategy", "normal");
        ChromeOptions opt = new ChromeOptions();
        opt.merge(dcap);
        WebDriver driver = new ChromeDriver(opt);
        driver.get("https://www.google.com/");
        System.out.println(driver.getTitle());
        driver.quit();
    }
}

  • 使用 ChromeOptions 类:

    package demo; //replace by your own package name
    
    import org.openqa.selenium.PageLoadStrategy;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    
    
    public class A_Chrome_Options_test {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe");
            ChromeOptions opt = new ChromeOptions();
            opt.setPageLoadStrategy(PageLoadStrategy.NORMAL);
            WebDriver driver = new ChromeDriver(opt);
            driver.get("https://www.google.com/");
            System.out.println(driver.getTitle());
            driver.quit();
        }
    }
    

  • 注意 : pageLoadStrategynormal, eagernoneWebDriver W3C Editor's DraftpageLoadStrategy 值作为 eager 仍然是一个WIP(Work InChromeDriver 实施中的进展).您可以在 渴望"页面加载中找到详细讨论Python 中 Chromedriver Selenium 的策略解决方法

    Note : pageLoadStrategy values normal, eager and none is a requirement as per WebDriver W3C Editor's Draft but pageLoadStrategy value as eager is still a WIP (Work In Progress) within ChromeDriver implementation. You can find a detailed discussion in "Eager" Page Load Strategy workaround for Chromedriver Selenium in Python

    <小时>

    参考资料:


    References:

    这篇关于Chrome 驱动的页面加载策略(更新到 Selenium v​​3.12.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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