Chrome WebDriver Test with Selenium Java API

Q

How to create a test program using Chrome WebDriver with Selenium Java API?

✍: FYIcenter.com

A

Here is a simple test program that the Chrome WebDriver Test with the Selenium Java API:

C:\fyicenter> type ChromeTest.java

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
public class ChromeTest {
  public static void main(String[] args) {
     WebDriver driver = new ChromeDriver();
     driver.get("http://www.google.com");
     driver.quit();
  }
}

Compile and run it with the Selenium Client JAR file:

C:\fyicenter> javac -classpath \
   \fyicenter\selenium\java\client-combined-3.141.59.jar \
   ChromeTest.java

C:\fyicenter> java -classpath \
   .;\fyicenter\selenium\java\client-combined-3.141.59.jar;\
   \fyicenter\selenium\java\libs\guava-25.0-jre.jar;\
   \fyicenter\selenium\java\libs\okhttp-3.11.0.jar;\
   \fyicenter\selenium\java\libs\okio-1.14.0.jar;\
   \fyicenter\selenium\java\libs\commons-exec-1.3.jar \
   ChromeTest

Starting ChromeDriver 2.16.333243 (0bfa1d3575fc1044244f21ddb82bf870944ef961) on port 44239
Only local connections are allowed.
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: session not created exception
from unknown error: Runtime.executionContextCreated has invalid 'context': 
   {"auxData":{"frameId":"E21148227BBAC982FC87783A806149E3","isDefault":true,"type":"default"},
   "id":1,"name":"","origin":"://"}
  (Session info: chrome=74.0.3729.169)
  (Driver info: chromedriver=2.16.333243 (0bfa1d3575fc1044244f21ddb82bf870944ef961),platform=Windows NT 10.0 x86_64) 
  (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 3.21 seconds
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '12.0.1'
Driver info: driver.version: ChromeDriver
   at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
   at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
   at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
   at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
   at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
   at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
   at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
   ...    

As you can see, we are not able to create the session: "session not created exception". The default ChromeDriver version is 2.16, which is not compatible with Selenium WebDriver 3.141. See next tutorial on how to fix it.

 

Use Chrome WebDriver with Google ChromeDriver

Compatibility of Selenium Client Java API and JDK

Starting with Selenium WebDriver Client Java API

⇑⇑ Selenium Tutorials

2020-02-07, 688👍, 0💬