使用selenium-java-4.0.0和chromedriver v85时出现超时异常。

huangapple go评论82阅读模式
英文:

Timeout exception when using dev tools with selenium-java-4.0.0 and chromedriver v85

问题

我正在尝试使用 Selenium 开发工具的 Java API,但是在多个 API 方法中,我遇到了 java.util.concurrent.TimeoutException 错误。

例如,我正在尝试使用 Network.clearBrowserCache,根据 chromedriver 文档(https://chromedevtools.github.io/devtools-protocol/tot/Network/)应该是可以工作的。

我正在使用以下代码调用 clearBrowserCache

chromeDriver.getDevTools().send(Network.clearBrowserCache())

但是这会失败,同时,如果我使用其他的 devTools 命令,例如:

chromeDriver.getDevTools().send(Browser.getVersion())

它会正常返回数据。

  • Chrome 版本为:85.0.4183.39
  • Chromedriver 版本为:85.0.4183.87
  • Selenium-java 版本为:4.0.0-alpha-6
英文:

I'm trying to use selenium dev tools java API, and for multiple API methods I'm getting java.util.concurrent.TimeoutException.

For example I'm trying to use Network.clearBrowserCache, which should work accroding to chromedriver docs: https://chromedevtools.github.io/devtools-protocol/tot/Network/

I'm calling clearBrowserCache using following code:
chromeDriver.getDevTools().send(Network.clearBrowserCache())

It fails, but at the same time if I use other devTools commands like this:
chromeDriver.getDevTools().send(Browser.getVersion())

It returns data properly.

  • Chrome version is: 85.0.4183.39
  • Chromedriver version is: 85.0.4183.87
  • Selenium-java version is: 4.0.0-alpha-6

答案1

得分: 6

尝试在调用 clearBrowserCache 之前调用 createSession

在您的设置中,这个有效:

chromeDriver.getDevTools().createSession();
chromeDriver.getDevTools().send(Network.clearBrowserCache())

而这个会产生 java.util.concurrent.TimeoutException

chromeDriver.getDevTools().send(Network.clearBrowserCache())

您可以使用以下代码片段来验证浏览器缓存是否已清除:

ChromeDriver driver = new ChromeDriver();
driver.get("https://refreshyourcache.com/en/cache-test/");
Thread.sleep(2000);
driver.getDevTools().createSession();
driver.getDevTools().send(Network.clearBrowserCache());
driver.get("https://refreshyourcache.com/en/cache-test/");
Thread.sleep(5000);

如果您运行这段代码,测试浏览器中显示的页面将会显示这些图片:

使用selenium-java-4.0.0和chromedriver v85时出现超时异常。

如果将这行代码注释掉 driver.getDevTools().send(Network.clearBrowserCache());,则会得到不同的结果:

使用selenium-java-4.0.0和chromedriver v85时出现超时异常。

英文:

Try calling createSession before calling clearBrowserCache.

Using your setup, this works:

chromeDriver.getDevTools().createSession();
chromeDriver.getDevTools().send(Network.clearBrowserCache())

and this produces java.util.concurrent.TimeoutException:

chromeDriver.getDevTools().send(Network.clearBrowserCache())

<br>

You can verify that the browser cache has been cleared with this snippet:

	ChromeDriver driver = new ChromeDriver();
	driver.get(&quot;https://refreshyourcache.com/en/cache-test/&quot;);
	Thread.sleep(2000);
	driver.getDevTools().createSession();
	driver.getDevTools().send(Network.clearBrowserCache());
	driver.get(&quot;https://refreshyourcache.com/en/cache-test/&quot;);
	Thread.sleep(5000);

If you run the code, the pages displayed in the test browser will show these images:
使用selenium-java-4.0.0和chromedriver v85时出现超时异常。

If you commment out the line driver.getDevTools().send(Network.clearBrowserCache()); then you get a different result:
使用selenium-java-4.0.0和chromedriver v85时出现超时异常。

答案2

得分: 3

使用 Selenium 4.0.0-alpha-6Chrome v85ChromeDriver v85.0 通过 [tag:google-chrome-devtools],您可以按以下方式使用 getVersion() 方法:

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.browser.Browser;

public class BrowserGetVersion {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized");
        ChromeDriver driver = new ChromeDriver(options);
        DevTools devTools = driver.getDevTools();
        devTools.createSession();
        devTools.send(Browser.getVersion());
    }
}

类似地,使用 clearBrowserCache() 方法,您可以使用以下代码块清除浏览器缓存:

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.network.Network;

public class ClearChromeCache {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized");
        ChromeDriver driver = new ChromeDriver(options);
        DevTools devTools = driver.getDevTools();
        devTools.createSession();
        devTools.send(Network.clearBrowserCache());
        driver.get("https://www.google.com/");
    }
}

附加注意事项

此外,您还可以使用 setCacheDisabled(true) 来完全 禁用 缓存,代码如下:

import java.util.Collections;
import java.util.Optional;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.network.Network;
import org.testng.Assert;
import org.testng.annotations.Test;

public class testngBasic {

    @Test
    public void foo() {
        System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized");
        options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
        options.setExperimentalOption("useAutomationExtension", false);
        ChromeDriver driver = new ChromeDriver(options);
        DevTools devTools = driver.getDevTools();
        devTools.createSession();
        devTools.send(Network.clearBrowserCache());
        devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.of(100000000)));
        devTools.send(Network.setCacheDisabled(true));
        devTools.addListener(Network.responseReceived(), responseReceived -> Assert.assertEquals(false, responseReceived.getResponse().getFromDiskCache()));
        driver.get("https://www.google.com/");
  }
}

此用例

可能您的代码与 java.util.concurrent.TimeoutException 错误无关,真正的问题可能出现在:

  • jdk 版本
  • guava 版本

解决方案

请确保:

  • JDK 升级到当前版本 JDK 8u252
  • guava 升级到 guava-29.0-jre

结束语

在 Selenium Chrome Driver 中禁用缓存

英文:

Using Selenium 4.0.0-alpha-6, Chrome v85 and ChromeDriver v85.0 through [tag:google-chrome-devtools] you must be able to use getVersion() method as follows:

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.browser.Browser;

public class BrowserGetVersion {
	public static void main(String[] args) {
		System.setProperty(&quot;webdriver.chrome.driver&quot;,&quot;C:\\WebDrivers\\chromedriver.exe&quot;);
		ChromeOptions options = new ChromeOptions();
		options.addArguments(&quot;--start-maximized&quot;);
		ChromeDriver driver = new ChromeDriver(options);
		DevTools devTools = driver.getDevTools();
		devTools.createSession();
		devTools.send(Browser.getVersion());
	}
}

Similarly, using the clearBrowserCache() method you should be able to clear the browser cache using the following code block:

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.network.Network;

public class ClearChromeCache {
	public static void main(String[] args) {
		System.setProperty(&quot;webdriver.chrome.driver&quot;,&quot;C:\\WebDrivers\\chromedriver.exe&quot;);
		ChromeOptions options = new ChromeOptions();
		options.addArguments(&quot;--start-maximized&quot;);
		ChromeDriver driver = new ChromeDriver(options);
		DevTools devTools = driver.getDevTools();
		devTools.createSession();
		devTools.send(Network.clearBrowserCache());
		driver.get(&quot;https://www.google.com/&quot;);
	}
}

Additional Consideration

Additionally, you can also use setCacheDisabled(true) to completely disable the cache as follows:

Code Block:

import java.util.Collections;
import java.util.Optional;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.network.Network;
import org.testng.Assert;
import org.testng.annotations.Test;

public class testngBasic {

	@Test
	public void foo() {
		System.setProperty(&quot;webdriver.chrome.driver&quot;,&quot;C:\\WebDrivers\\chromedriver.exe&quot;);
		ChromeOptions options = new ChromeOptions();
		options.addArguments(&quot;--start-maximized&quot;);
		options.setExperimentalOption(&quot;excludeSwitches&quot;, Collections.singletonList(&quot;enable-automation&quot;));
		options.setExperimentalOption(&quot;useAutomationExtension&quot;, false);
		ChromeDriver driver = new ChromeDriver(options);
		DevTools devTools = driver.getDevTools();
		devTools.createSession();
		devTools.send(Network.clearBrowserCache());
		devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.of(100000000)));
		devTools.send(Network.setCacheDisabled(true));
		devTools.addListener(Network.responseReceived(), responseReceived -&gt; Assert.assertEquals(false, responseReceived.getResponse().getFromDiskCache()));
		driver.get(&quot;https://www.google.com/&quot;);

  }
}

This usecase

Possibly your code have nothing to do with java.util.concurrent.TimeoutException error and the real issue is either with the:

  • jdk version
  • guava version

Solution

Ensure that:

  • JDK is upgraded to current levels JDK 8u252.
  • guava is upgraded to guava-29.0-jre.

Outro

Disable cache in Selenium Chrome Driver

答案3

得分: 2

它运行得很好

    public void testCdt {
        final ChromeLauncher launcher = new ChromeLauncher();
        final ChromeService chromeService = launcher.launch(false);
        final ChromeTab tab = chromeService.createTab();
        final ChromeDevToolsService devToolsService = chromeService.createDevToolsService(tab);
        final Page page = devToolsService.getPage();
        Network network = devToolsService.getNetwork();
        // 清除浏览器缓存
        network.clearBrowserCache();
        // 使用onRequestWillBeSent事件处理程序记录请求。
        network.onRequestWillBeSent(
                event ->
                        System.out.printf(
                                "请求:%s %s%s",
                                event.getRequest().getMethod(),
                                event.getRequest().getUrl(),
                                System.lineSeparator()));
    
        network.onLoadingFinished(
                event -> {
                  chromeService.closeTab(tab);
                  launcher.close();
                });
        network.enable();
        page.navigate("http://github.com");
        devToolsService.waitUntilClosed();
      }
英文:

It worked fine

public void testCdt {
    final ChromeLauncher launcher = new ChromeLauncher();
    final ChromeService chromeService = launcher.launch(false);
    final ChromeTab tab = chromeService.createTab();
    final ChromeDevToolsService devToolsService = chromeService.createDevToolsService(tab);
    final Page page = devToolsService.getPage();
    Network network = devToolsService.getNetwork();
    // Clear browser cached
    network.clearBrowserCache();
    // Log requests with onRequestWillBeSent event handler.
    network.onRequestWillBeSent(
            event -&gt;
                    System.out.printf(
                            &quot;request: %s %s%s&quot;,
                            event.getRequest().getMethod(),
                            event.getRequest().getUrl(),
                            System.lineSeparator()));

    network.onLoadingFinished(
            event -&gt; {
              chromeService.closeTab(tab);
              launcher.close();
            });
    network.enable();
    page.navigate(&quot;http://github.com&quot;);
    devToolsService.waitUntilClosed();
  }

huangapple
  • 本文由 发表于 2020年9月10日 21:12:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63830485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定