Chrome 驱动程序的页面加载策略(更新至 Selenium v​​3.12.0)

Page load strategy for Chrome driver (Updated till Selenium v3.12.0)

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

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

在 FireFox 中我知道 PAGE_LOAD_STRATEGY = "eager"

chrome有类似的东西吗?

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

尝试使用显式等待。访问 this link。可能会有帮助

也试试这个代码:

WebDriver driver = new FirefoxDriver();
String startURL = //a starting url;
String currentURL = null;
WebDriverWait wait = new WebDriverWait(driver, 10);

foo(driver,startURL);

/* go to next page */
if(driver.findElement(By.xpath("//*[@id='someID']")).isDisplayed()){
    String previousURL = driver.getCurrentUrl();
    driver.findElement(By.xpath("//*[@id='someID']")).click();  
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    ExpectedCondition e = new ExpectedCondition<Boolean>() {
          public Boolean apply(WebDriver d) {
            return (d.getCurrentUrl() != previousURL);
          }
        };

    wait.until(e);
    currentURL = driver.getCurrentUrl();
    System.out.println(currentURL);
}   

我希望使用上面的代码可以解决你的问题

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

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


来自 Webdriver 规范:

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 等)时,您可以更改 pageLoadStrategy 通过 webdriver.

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

  1. normal

    此策略导致 Selenium 等待整个页面加载(html 内容和子资源已下载并解析)。

  2. eager

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

  3. none

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

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


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

  • 使用 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();
        }
    }
    
  • 使用Chrome选项 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


参考文献:

在 C# 中,由于 PageLoadStrategy.Eager 似乎不适用于 Chrome,我只是自己用 WebDriverWait 编写了它。将 PageLoadStrategy 设置为 none 然后这样做基本上会覆盖它:

new WebDriverWait(_driver, TimeSpan.FromSeconds(20))
    .Until(d =>
    {
      var result = ((IJavaScriptExecutor) d).ExecuteScript("return document.readyState");
      return result.Equals("interactive") || result.Equals("complete");
    });

您只需添加 chrome 驱动程序作为参数,在我的例子中,TimeSpan 设置为最长 20 秒。因此它最多等待 20 秒让页面交互或完成

对于硒 4 和 Python

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.page_load_strategy = 'none'
driver = webdriver.Chrome(options=options)
driver.get("http://www.google.com")
driver.quit()

有关详细信息,请参见此处 https://www.selenium.dev/documentation/webdriver/capabilities/shared/#none