gpt4 book ai didi

java - 通过 Selenium 和 Java 使用 ChromeDriver 和 Chrome 在窗口之间切换时,从渲染器接收消息超时 : 300. 000 错误

转载 作者:行者123 更新时间:2023-12-01 18:43:29 24 4
gpt4 key购买 nike

我在使用 selenium 3.13.0 和 chrome 版本 79.0.3945.130 以及 java 时遇到问题。我编写此代码是为了测试必须在两个窗口之间切换的网页。当我开始运行这个脚本时,一切都正常,但是当驱动程序切换到新窗口时,它似乎不再可用!很长一段时间后,我得到了这个错误。有谁知道问题出在哪里吗?

(我确信程序的控制会脱离循环并切换到新窗口,但在离开循环后驱动程序似乎会丢失。)

一些函数,如 driver.quit() 或 driver.getWindowHandles() 在退出循环后起作用,但其他一些函数,如 driver.getPageSource() 或 driver.manage().window().maximize() 不起作用并导致这个错误。

这是我的示例代码:

public class myClassFirst {

public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","Driver_Path");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize(); //it works correctly
driver.get("Web_page_Url");
Thread.sleep(15000); //to load page
System.out.println("finish waiting ...");
/*
some script here
*/
// go to appropriate frame
driver.switchTo().defaultContent();
driver.switchTo().frame("Faci3");
driver.switchTo().frame(driver.findElement(By.name("Commander")));
Set<String> s = driver.getWindowHandles();
System.out.println(s); //print windowID of current window(1)
//select button element
WebElement prepareToPrint = driver.findElement(By.id("PreparePrint"));
prepareToPrint.click();
Thread.sleep(5000); //to load new window(2)
String sThis = driver.getWindowHandle(); //get windowID of current window(1)
for (String winId:driver.getWindowHandles()) {
if (!winId.equals(sThis)) {
driver.switchTo().window(winId); //switch to new window(2)
System.out.println(winId); //print windowID of new window(2)
break;
}
}
System.out.println("exit from the loop");
driver.manage().window().maximize(); //From here does not work anymore(real line 92)
Actions builder = new Actions(driver);
builder.contextClick().perform();
System.out.println("End = Finish");
driver.quit();
}
}

这是输出:

finish waiting ...
[CDwindow-9B003436EA7F9E41C56694893D686231]
CDwindow-7205BC1AD0022BD28BFD18FCD4FA379B
exit from the loop
[1579772104.277][SEVERE]: Timed out receiving message from renderer: 300.000
[1579772104.280][SEVERE]: Timed out receiving message from renderer: -0.003
Exception in thread "main" org.openqa.selenium.TimeoutException: timeout
(Session info: chrome=79.0.3945.130)
Build info: version: '3.13.0', revision: '2f0d292', time: '2018-06-25T15:32:14.902Z'
System info: host: 'DESKTOP-BT6SU1U', ip: '192.168.67.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_181'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 79.0.3945.130, chrome: {chromedriverVersion: 79.0.3945.36 (3582db32b3389..., userDataDir: C:\Users\Sadell\AppData\Loc...}, goog:chromeOptions: {debuggerAddress: localhost:61628}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 5b36cc7b69534ede85d7c957306c2626
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
at org.openqa.selenium.remote.RemoteWebDriver.perform(RemoteWebDriver.java:614)
at org.openqa.selenium.interactions.Actions$BuiltAction.perform(Actions.java:638)
at org.openqa.selenium.interactions.Actions.perform(Actions.java:594)
at myClassFirst.main(myClassFirst.java:92)

Process finished with exit code 1

最佳答案

Selenium WebDriver 默认情况下会等待页面完全加载。但是,您可以告诉它不要这样做,或者使用另一个 PageLoadStrategy。

以下内容应该对您有帮助。

ChromeOptions options = new ChromeOptions();
options.setPageLoadStrategy(PageLoadStrategy.NONE);

当我使用 EdgeDriver 遇到同样的问题时,这篇文章帮助了我。 https://www.skptricks.com/2018/08/timed-out-receiving-message-from-renderer-selenium.html

关于java - 通过 Selenium 和 Java 使用 ChromeDriver 和 Chrome 在窗口之间切换时,从渲染器接收消息超时 : 300. 000 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59877565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com