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

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

问题描述

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

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

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

chrome 有没有类似的东西?

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

解决方案

ChromeDriver 77.0(支持 Chrome 77 版)现在支持 eager作为pageLoadStrategy.

<块引用>

已解决问题 1902:支持急切页面加载策略 [Pri-2]

<小时>

来自 Webdriver 规范:

<块引用>

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

页面加载花费太多时间并且您需要停止下载额外的子资源(图片、css、js等)时,您可以更改pageLoadStrategy 通过 webdriver.

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

  1. 普通

    此状态导致 Selenium 等待整个页面加载(下载和解析的 html 内容和子资源).

  2. 渴望

    此状态导致 Selenium 等待 DOMContentLoaded 事件(仅下载和解析 html 内容).

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

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

<小时>

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

  • 使用DesiredCapabilities类:

    打包演示;//替换为你自己的包名导入 org.openqa.selenium.WebDriver;导入 org.openqa.selenium.chrome.ChromeDriver;导入 org.openqa.selenium.chrome.ChromeOptions;导入 org.openqa.selenium.remote.DesiredCapabilities;公共类 A_Chrome_DCap_Options {公共静态无效主(字符串 [] args){System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe");DesiredCapabilities dcap = 新的 DesiredCapabilities();dcap.setCapability("pageLoadStrategy", "正常");ChromeOptions opt = new ChromeOptions();opt.merge(dcap);WebDriver 驱动程序 = new ChromeDriver(opt);driver.get("https://www.google.com/");System.out.println(driver.getTitle());驱动程序退出();}}

  • 使用 ChromeOptions 类:

    打包演示;//替换为你自己的包名导入 org.openqa.selenium.PageLoadStrategy;导入 org.openqa.selenium.WebDriver;导入 org.openqa.selenium.chrome.ChromeDriver;导入 org.openqa.selenium.chrome.ChromeOptions;公共类 A_Chrome_Options_test {公共静态无效主(字符串 [] args){System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe");ChromeOptions opt = new ChromeOptions();opt.setPageLoadStrategy(PageLoadStrategy.NORMAL);WebDriver 驱动程序 = new ChromeDriver(opt);driver.get("https://www.google.com/");System.out.println(driver.getTitle());驱动程序退出();}}

<块引用>

注意 : pageLoadStrategy values normal, eagernone<的要求strong>WebDriver W3C Editor's DraftpageLoadStrategy 值为 eager 仍然是一个WIP(Work InChromeDriver 实施中的进展).您可以在 Eager"页面加载中找到详细讨论Python 中 Chromedriver Selenium 的策略变通方法

<小时>

参考文献:

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.

In FireFox I know about PAGE_LOAD_STRATEGY = "eager".

Is there something similar for chrome?

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 (which supports Chrome version 77) now supports eager as pageLoadStrategy.

Resolved issue 1902: Support eager page load strategy [Pri-2]


From the Webdriver specs:

For commands that cause a new document to load, the point at which the command returns is determined by the session’s page loading strategy.

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.

As of this writing, pageLoadStrategy supports the following values :

  1. normal

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

  2. eager

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

  3. none

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

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


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

  • Using DesiredCapabilities Class :

    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();
        }
    }
    

  • Using ChromeOptions Class :

    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();
        }
    }
    

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天全站免登陆