英文:
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);
如果您运行这段代码,测试浏览器中显示的页面将会显示这些图片:
如果将这行代码注释掉 driver.getDevTools().send(Network.clearBrowserCache());
,则会得到不同的结果:
英文:
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("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);
If you run the code, the pages displayed in the test browser will show these images:
If you commment out the line driver.getDevTools().send(Network.clearBrowserCache());
then you get a different result:
答案2
得分: 3
使用 Selenium 4.0.0-alpha-6、Chrome v85 和 ChromeDriver 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("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());
}
}
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("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/");
}
}
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("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/");
}
}
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
答案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 ->
System.out.printf(
"request: %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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论