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

查看:165
本文介绍了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".

铬有类似的东西吗?

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:

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

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.

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

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. normal

  1. normal

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

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

eager

eager

这种状态导致Selenium等待DOMContentLoaded事件(仅下载并解析了html内容).

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

none

none

此策略使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.

这是用于配置 <通过 DesiredCapabilities 类和 ChromeOptions 类的一个实例来实现strong> pageLoadStrategy() ::

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

  • 注意:pageLoadStrategy normal eager none 是根据> WebDriver W3C编辑器草案 的要求,但pageLoadStrategy的值为 eager 仍然是 ChromeDriver 实施中的 WIP(在进行中).您可以在渴望"的页面加载中找到详细的讨论适用于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:

    • WebDriver navigation
    • WebDriver page load strategies
    • WhatWG Document readyStateChange / readiness

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

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