问题
Below is a snippet of code I'm using to initialize chrome webdriver:
System.out.println("Initializing Chrome");
ChromeOptions options = new ChromeOptions();
//options.setBinary("C:/Program Files (x86)/Google/Chrome/Application");
options.addArguments("start-maximized");
options.setExperimentalOption("excludeSwitches",new String[]{"enable-automation"});
options.addArguments("--disable-extensions");
options.addArguments("--disable-infobars");
options.addArguments("--allow-running-insecure-content");
WDriver = new ChromeDriver(options);
//WDriver = new ChromeDriver();
WDriver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
System.out.println("Chrome Initialization Complete");
But it seems to be opening two instances of chrome, this is displayed on the console:
Initializing Chrome
Starting ChromeDriver 78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-
heads/3904@{#800}) on port 12262
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious
code.
Nov 04, 2019 3:29:38 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
log4j:WARN No appenders could be found for logger
(org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[1572910181.451][WARNING]: Timed out connecting to Chrome, retrying...
Nov 04, 2019 3:29:43 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Falling back to original OSS JSON Wire Protocol.
[1572910186.002][WARNING]: Timed out connecting to Chrome, retrying...
Nov 04, 2019 3:29:48 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Chrome Initialization Complete
Chrome Version: 78.0.3904.87
Chromedriver Version: chromedriver_78.0.3904.70
Selenium Version: 3.141.59
While the issue is not a major roadblock it is a bit annoying that it opens a 2nd instance of chrome that will eat memory while it is on standby.
Cheers.
回答1:
There are a couple of things you need to take care:
This error message...
Nov 04, 2019 3:29:38 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
...implies that the ChromeDriver was unable to initiate/spawn a new WebBrowser i.e. Chrome Browser session.
As per the discussion Attempting bi-dialect session, assuming Postel's Law holds true on the remote end thread 'webdriver dispatcher' panicked at 'index out of bounds: the len is 0 but the index is 0 this issue was observed with Selenium Client v3.0.0-beta3 which was released on 2016-09-01 14:57:03 -0700.
Simon in a comment mentioned that:
The root cause was a
ClassCastException
. We now catch that exception, log the thing that we were trying to parse and continue with other attempts to complete the handshake. The fix was available in Selenium Client v3.0.0-beta4.
The most appropriate solution would have been to:
- Upgrade Selenium to current level of Version 3.141.59.
You can find a detailed discussion in org.openqa.selenium.remote.ProtocolHandshake createSession INFORMATION: Attempting bi-dialect session with Selenium Grid
The next error message...
Nov 04, 2019 3:29:43 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Falling back to original OSS JSON Wire Protocol.
[1572910186.002][WARNING]: Timed out connecting to Chrome, retrying...
Nov 04, 2019 3:29:48 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
...also implies that the ChromeDriver/Chrome Browser combo you are using is not the recent one as the current implementation of ChromeDriver follows W3C and initial logs are as follows:
Starting ChromeDriver 78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-heads/3904@{#800}) on port 9626
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
Nov 05, 2019 3:41:53 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Solution
Ensure that:
- JDK is upgraded to current levels JDK 8u222.
- Selenium is upgraded to current levels Version 3.141.59.
- ChromeDriver is updated to current ChromeDriver v78.0 level.
- Chrome is updated to current Chrome Version 78.0 level. (as per ChromeDriver v78.0 release notes)
- Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
- (WindowsOS only) Use CCleaner tool to wipe off all the OS chores before and after the execution of your Test Suite.
- (LinuxOS only) Free Up and Release the Unused/Cached Memory in Ubuntu/Linux Mint before and after the execution of your Test Suite.
- If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client.
- Take a System Reboot.
- Execute your
@Test
as non-root user. - Always invoke
driver.quit()
withintearDown(){}
method to close & destroy the WebDriver and Web Client instances gracefully.
References
You can find some relevant discussion in:
- Issue 1504: Unable to Open latest chrome using latest Selenium.
- Issue 1451: Chrome 54+ session not created exception: Runtime.executionContextCreated has invalid 'context'
Outro
As per this jar-download.com article the createSession()
in ProtocolHandshake.java
file of Selenium v3.0.1 contained the following:
public Result createSession(HttpClient client, Command command)
throws IOException {
// Avoid serialising the capabilities too many times. Things like profiles are expensive.
Capabilities desired = (Capabilities) command.getParameters().get("desiredCapabilities");
desired = desired == null ? new DesiredCapabilities() : desired;
Capabilities required = (Capabilities) command.getParameters().get("requiredCapabilities");
required = required == null ? new DesiredCapabilities() : required;
String des = new BeanToJsonConverter().convert(desired);
String req = new BeanToJsonConverter().convert(required);
// Assume the remote end obeys the robustness principle.
StringBuilder parameters = new StringBuilder("{");
amendW3CParameters(parameters, des, req);
parameters.append(",");
amendOssParamters(parameters, des, req);
parameters.append("}");
LOG.info("Attempting bi-dialect session, assuming Postel's Law holds true on the remote end");
Optional result = createSession(client, parameters);
// Assume a fragile OSS webdriver implementation
if (!result.isPresent()) {
parameters = new StringBuilder("{");
amendOssParamters(parameters, des, req);
parameters.append("}");
LOG.info("Falling back to original OSS JSON Wire Protocol.");
result = createSession(client, parameters);
}
// Assume a fragile w3c implementation
if (!result.isPresent()) {
parameters = new StringBuilder("{");
amendW3CParameters(parameters, des, req);
parameters.append("}");
LOG.info("Falling back to straight W3C remote end connection");
result = createSession(client, parameters);
}
if (result.isPresent()) {
Result toReturn = result.get();
LOG.info(String.format("Detected dialect: %s", toReturn.dialect));
return toReturn;
}
throw new SessionNotCreatedException(
String.format(
"Unable to create new remote session. " +
"desired capabilities = %s, required capabilities = %s",
desired,
required));
}
来源:https://stackoverflow.com/questions/58702552/attempting-bi-dialect-session-assuming-postels-law-holds-true-on-the-remote-en