webdriver selenium获取浏览器network请求

获取浏览器network请求

1.下载chromedriver,需要和chrome版本一致,解压到目录/Users/***/
https://chromedriver.chromium.org/downloads
2.代码如下

public static void main(String[] args) {
    LoggingPreferences logPrefs =new LoggingPreferences();
    logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
    DesiredCapabilities d = DesiredCapabilities.chrome();
    d.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);

    System.setProperty("webdriver.chrome.driver", "/Users/***/chromedriver");

    WebDriver driver =new ChromeDriver(d);
    driver.get("https://www.google.com");
    TimeUtil.toWait(5000);

    driver.findElements(By.xpath("//a[contains(@class,'id:001')]")).get(1).click();  //模拟点击后打开新tab窗口页面
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    LogEntries les = driver.manage().logs().get(LogType.PERFORMANCE);
    for (LogEntry le : les) {
        System.out.println(le.getMessage());
        if(le.toString().contains("test_target_url?")) {
                  System.out.println(le.getMessage());
        }
}

你可能感兴趣的:(webdriver selenium获取浏览器network请求)