Selenium: Unable to Locate Browser Binaries on Linux - java

I'm currently trying to use Selenium to automate some browsing tasks on Linux (I'm running Linux Mint 20). However, I've ran into a brick wall: I can't get it to locate any browser binaries. I've tried with Firefox and Chromium (thinking that if I could get the Chromium binary to load I'd live with that), but they both yield the same result, Selenium says it can't find the browser binary.
Here is my code:
package test1;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class app
{
public static void main(String[] args)
{
System.setProperty("webdriver.firefox.driver", "/home/ann/bin/geckodriver");
WebDriver driver = new FirefoxDriver();
}
}
Here are the results upon running the code:
Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: LINUX
Build info: version: '4.0.0-alpha-7', revision: 'de8579b6d5'
System info: host: 'ann-System-Product-Name', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '5.4.0-58-generic', java.version: '11.0.8'
Driver info: driver.version: FirefoxDriver
at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:95)
at java.base/java.util.Optional.orElseGet(Optional.java:369)
at org.openqa.selenium.firefox.FirefoxOptions.getBinary(FirefoxOptions.java:199)
at org.openqa.selenium.firefox.GeckoDriverService$Builder.withOptions(GeckoDriverService.java:180)
at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:207)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:177)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:155)
at test1.app.main(app.java:10)
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Firefox is installed and can be executed from the terminal:
ann#ann-System-Product-Name:~$ whereis firefox
firefox: /usr/bin/firefox /usr/lib/firefox /etc/firefox
Geckodriver is in my $PATH and executing it gives the following results:
ann#ann-System-Product-Name:~$ geckodriver
1609693404322 geckodriver INFO Listening on 127.0.0.1:4444

System.setProperty("webdriver.gecko.driver", "C:\\Users\\prave\\Downloads\\geckodriver.exe");
File pathBinary = new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
FirefoxBinary firefoxBinary = new FirefoxBinary(pathBinary);
FirefoxOptions options = new FirefoxOptions();
options.setBinary(firefoxBinary);
FirefoxDriver firefox=new FirefoxDriver(options);
System.setProperty("webdriver.firefox.driver", "/home/ann/bin/geckodriver"); sets the gekodriver path not firefox.exe path. You can use above code
and also its not webdriver.firefox.driver but webdriver.gecko.driver

GeckoDriver / Firefox
On linux platforms the default location of firefox binary is:
/usr/bin/firefox
If firefox is installed at the default location you can simply:
System.setProperty("webdriver.gecko.driver", "/home/ann/bin/geckodriver");
WebDriver driver = new FirefoxDriver();
However, the default location may vary for some of the linux distros. In those cases and in case firefox is installed at a customized location you have to:
System.setProperty("webdriver.gecko.driver", "/home/ann/bin/geckodriver");
FirefoxOptions options = new FirefoxOptions();
options.setBinary("/path/to/firefox");
WebDriver driver = new FirefoxDriver(options);
driver.get("https://stackoverflow.com");
You can find a relevant detailed discussion in WebDriverException: Failed to connect to binary FirefoxBinary(C:\Program Files\Mozilla Firefox\firefox.exe) with GeckoDriver Firefox and Selenium Java
ChromeDriver / Chrome
Similarly on linux platforms the default location of chrome binary is:
/usr/bin/google-chrome
If google-chrome is installed at the default location you can simply:
System.setProperty("webdriver.chrome.driver", "/home/ann/bin/chromedriver");
WebDriver driver = new ChromeDriver();
However, the default location may vary for some of the linux distros. In those cases and in case chrome is installed at a customized location you have to:
System.setProperty("webdriver.chrome.driver", "/home/ann/bin/chromedriver");
ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/chrome");
WebDriver driver = new ChromeDriver(options);
driver.get("https://stackoverflow.com");
You can find a relevant detailed discussion in What is default location of ChromeDriver and for installing Chrome on Windows
Note: In case you are using geckodriver/firefox combo, instead of webdriver.firefox.driver you have to use webdriver.gecko.driver

Related

SessionNotCreatedException: Could not start a new session. Response code 500 error using Selenium Java and WebDriverManager through pom.xml

I want to test my script on chrome beta version and for that reason I have installed chrome beta version but somehow I am not able to start it using Selenium Java. I have all the needed dependency added in pom.xml file regarding webdriver manager etc. I am sharing my console error and also script.
ChromeOptions optionsBeta = new ChromeOptions();
optionsBeta.setBinary("C:\\Users\\WRP\\Downloads\\Programs\\ChromeSetup.exe");
System.setProperty("webdriver.chrome.driver", "C:\\Users\\WRP\\eclipse-workspace\\PracticeProject\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver(optionsBeta);
//System.setProperty("webdriver.chrome.driver", "./Driver/chromedriver.exe");
// WebDriver driver = new ChromeDriver();
WebDriverManager.chromedriver().setup();
//WebDriverManager.chromedriver().driverVersion("104.0.5112.29").setup();
//WebDriver driver = new ChromeDriver();
WebDriverWait waits=new WebDriverWait (driver, Duration.ofSeconds(10));
Error tracelogs:
Starting ChromeDriver 104.0.5112.29 (eff877e18f767c77fef0481a1cba402c8cbad404-refs/branch-heads/5112#{#422}) on port 55215
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: unknown error: Chrome failed to start: crashed.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location C:\Users\WRP\Downloads\Programs\ChromeSetup.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Build info: version: '4.3.0', revision: 'a4995e2c09*'
System info: host: 'AWAIS-PC', ip: '192.168.1.62', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '18.0.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [], binary: C:\Users\WRP\Downloads\Prog..., extensions: []}}], desiredCapabilities=Capabilities {browserName: chrome, goog:chromeOptions: {args: [], binary: C:\Users\WRP\Downloads\Prog..., extensions: []}}}]
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:144)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:102)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:67)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:156)
at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:569)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:264)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:179)
at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:101)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:81)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:70)
at First_Test.Practice_First.main(Practice_First.java:29)
This error message...
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: unknown error: Chrome failed to start: crashed.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location C:\Users\WRP\Downloads\Programs\ChromeSetup.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. google-chrome session.
Your have to take care of a couple of things here as follows:
setBinary() argument should be used to point to the binary executable i.e. chrome.exe post installation of the browser software (ChromeSetup.exe)
optionsBeta.setBinary("C:\\location\\to\\chrome.exe");
You'd use either of the following (not both):
The downloaded version of the chromedriver.exe using the System.setProperty() line as follows:
System.setProperty("webdriver.chrome.driver", "C:\\Users\\WRP\\eclipse-workspace\\PracticeProject\\Driver\\chromedriver.exe");
The WebDriverManager dependency in pom.xml and setup the chromedriver as:
WebDriverManager.chromedriver().setup();
Your effective code block will be:
Using downloaded ChromeDriver:
System.setProperty("webdriver.chrome.driver", "C:\\Users\\WRP\\eclipse-workspace\\PracticeProject\\Driver\\chromedriver.exe");
ChromeOptions optionsBeta = new ChromeOptions();
optionsBeta.setBinary("C:\\location\\to\\chrome.exe");
WebDriver driver = new ChromeDriver(optionsBeta);
Using Maven Dependency in pom.xml:
WebDriverManager.chromedriver().driverVersion("104.0.5112.29").setup();
ChromeOptions optionsBeta = new ChromeOptions();
optionsBeta.setBinary("C:\\location\\to\\chrome.exe");
WebDriver driver = new ChromeDriver(optionsBeta);
Use the below chrome option
options = webdriver.ChromeOptions()
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--ignore-ssl-errors=yes')
options.add_argument('--ignore-certificate-errors')
This line very important on skip this error
options.add_argument('--disable-dev-shm-usage')

Hi, i have integrate my selenium script with jenkins but i am not able to launch the chrome browser

i have integrate my selenium script with jenkins but i am not able to launch the chrome browser, i have tried almost every solutions and in my machine there is chrome version Version 75.0.3770.100 (Official Build) (64-bit) and 75 version supported chrome driver is available.
in jenkins i am getting
Starting ChromeDriver 75.0.3770.90
(a6dcaf7e3ec6f70a194cc25e8149475c6590e025-refs/branch-heads/3770#{#1003})
on port 21983 Only local connections are allowed. Please protect ports
used by ChromeDriver and related test frameworks to prevent access by
malicious code. Tests run: 7, Failures: 1, Errors: 0, Skipped: 6, Time
elapsed: 1.332 sec <<< FAILURE! - in TestSuite
beforeMethod(qa.Vehicle_registration) Time elapsed: 1.206 sec <<<
FAILURE! org.openqa.selenium.WebDriverException: unknown error:
Chrome failed to start: exited abnormally (unknown error:
DevToolsActivePort file doesn't exist) (The process started from
chrome location /usr/bin/google-chrome is no longer running, so
ChromeDriver is assuming that Chrome has crashed.) Build info:
version: '3.5.3', revision: 'a88d25fe6b', time:
'2017-08-29T12:42:44.417Z' System info: host:
'administrator-Latitude-3480', ip: '127.0.1.1', os.name: 'Linux',
os.arch: 'amd64', os.version: '4.15.0-52-generic', java.version:
'1.8.0_11' Driver info: driver.version: ChromeDriver remote
stacktrace: #0 0x55c60975b6e9
I have tried below code but still i am getting same error.
public class Google{
WebDriver driver;
#BeforeClass
public void beforeMethod() throws IOException, InterruptedException
{
System.setProperty("webdriver.chrome.driver","/home/ashishtiwari/driver/chromedriver");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("start-maximized"); // open Browser in maximized mode
options.addArguments("disable-infobars"); // disabling infobars
options.addArguments("--disable-extensions"); // disabling extensions
options.addArguments("--disable-gpu"); // applicable to windows os only
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
options.addArguments("--no-sandbox"); // Bypass OS security model
options.addArguments("--headless");
options.addArguments("--disable-dev-shm-usage");
driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(utill.ashu, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(utill.sonam, TimeUnit.SECONDS);
//http://tmsapp.xbees.in/#/dashboard
driver.get("https://google.com");
}
}
I got the same problem while launching the chrome browser using selenium in visual studio code.I solved this issue by deleting chromedriver.exe from my project as I created so many Chromedriver.exe file.Just check is there more than one chromedriver.exe file is there in your project.
chromedriver.exe file should be in bin/debug/net5.0/chromedriver.exe.
If you follow above steps then there is no need to give the local path for chrome driver.

SessionNotCreatedException: session not created exception from unknown error: Runtime.executionContextCreated has invalid 'context' with Chrome driver

Error stack trace (Updated from comments):
Starting ChromeDriver 2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067) on port 7778 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":"961185F0AA38D24650EF6C797BC32535","isDefault":true,"type":"default"},"id":1,"name":"","origin":"://"}
(Session info: chrome=70.0.3538.102)
(Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 1.68 seconds Build info: version: '3.141.5', revision: 'd54ebd709a', time: '2018-11-06T11:58:41'
System info: host: 'LTAH024', ip: '192.168.131.142', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_60'
Driver info: driver.version: ChromeDriver
I wrote simple program to launching a chrome browser. Please see the below code. I have already set a path in environment variable:
package automationFramework;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ChromeBrowser {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver drive = new ChromeDriver();
drive.get("http://toolsqa.com/selenium-webdriver/running-tests-in-chrome-browser/");
System.out.println("Successfully open tools qa website in Chrome browser");
//Thread.sleep(5000); //To initiate thread , we need to add throws interrupt exception
//Close the driver
//driver.quit();
}
}
Please look into this and help me out. The same thing geckodriver for firefox is working.
This error message...
Starting ChromeDriver 2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067) on port 7778 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":"961185F0AA38D24650EF6C797BC32535","isDefault":true,"type":"default"},"id":1,"name":"","origin":"://"}
(Session info: chrome=70.0.3538.102)
(Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 1.68 seconds Build info: version: '3.141.5', revision: 'd54ebd709a', time: '2018-11-06T11:58:41'
System info: host: 'LTAH024', ip: '192.168.131.142', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_60'
Driver info: driver.version: ChromeDriver
...implies that the ChromeDriver was unable to initiate/spawn a new WebBrowser i.e. Chrome Browser session.
You have 3 issues exactly and your main issue is the incompatibility between the version of the binaries you are using as follows:
You are using chromedriver=2.2.20
Release Notes of chromedriver=2.20 clearly mentions the following :
Supports Chrome v43-48
You are using chrome=70.0
Release Notes of ChromeDriver v2.43 clearly mentions the following :
Supports Chrome v69-71
Your Selenium Client version is the current version of 3.141.5..
Your JDK version is 1.8.0_60 which is pretty ancient.
So there is a clear mismatch between the JDK v8u60 , Selenium Client v3.141.5 , ChromeDriver v2.20 and the Chrome Browser v70.0
Solution
While using Selenium v3.x clients you need to download the latest ChromeDriver from ChromeDriver - WebDriver for Chrome store it anywhere within your system and provide the absolute path of the ChromeDriver through System.setProperty() line as follows:
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
Upgrade JDK to recent levels JDK 8u191.
Upgrade ChromeDriver to current ChromeDriver v2.43 level.
Keep Chrome version between Chrome v69-71 levels. (as per ChromeDriver v2.43 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 through Revo Uninstaller and install a recent GA and released version of Web Client.
Take a System Reboot.
Execute your #Test.
Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.
Download chrome driver, keep it at your local and put the path at System.setProperty try the below code, hope it helps.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ChromeBrowser {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "B:\\chromedriver.exe"); //put driver path here
WebDriver drive = new ChromeDriver();
drive.get("http://toolsqa.com/selenium-webdriver/running-tests-in- chrome-browser/");
System.out.println("Successfully open tools qa website in Chrome browser");
drive.quit();
}
}
Their are Three Ways to open The Chrome Browser:
First one:using system.setproperty
System.setProperty("webdriver.chrome.driver", "F:\\New folder\\chromedriver.exe");
Webdriver driver = new ChromeDriver();
Second one : using Chrome Options:
//set path to chromedriver.exe
ChromeOptions options = new ChromeOptions();
options.setAcceptInsecureCerts(true);
options.setBinary(new File("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"));
options.addArguments("disable-infobars");
System.setProperty("webdriver.chrome.driver", "F:\\New folder\\chromedriver.exe");
driver = new ChromeDriver(options);
Last one : if you are using maven use this
This downloads the latest chrome driver version and starts it. You can use WebDriverManager within using bonigarcia dependency. Add The bonigarcia dependency in your Pom.xml File and start using it via WebdriverManager
https://github.com/bonigarcia/webdrivermanager
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
Finally What is Version for your Gecko driver & Firefox?

Selenium firefox - WebDriverException: Reached error page: about:certerror

Meta :-
Firefox v51.0.1 (32-bit)
Windows 10
Selenium 3.0.1
Geckodriver Win32 v0.13.0
Java v1.8.0_71
Steps to reproduce :-
WebDriver driver = new FirefoxDriver();
driver.get("untrusted/self-signed URL")
Stacktrace :-
org.openqa.selenium.WebDriverException: Reached error page: about:certerror?e=nssBadCert&u=xxxxxxxx&c=UTF-8&f=regular&d=xxxxxx%20uses%20an%20invalid%20security%20certificate.%0A%0AThe%20certificate%20is%20not%20trusted%20because%20it%20is%20self-signed.%0AThe%20certificate%20is%20not%20valid%20for%20the%20name%20xxxxxx%0A%0AError%20code%3A%20%3Ca%20id%3D%22errorCode%22%20title%3D%22SEC_ERROR_UNKNOWN_ISSUER%22%3ESEC_ERROR_UNKNOWN_ISSUER%3C/a%3E%0A
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:48:19 -0700'
System info: host: 'Saurabh-PC', ip: '192.168.3.8', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_71'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Screenshot :-
I have also tried using FirefoxProfile as :-
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
dc.setCapability(FirefoxDriver.PROFILE, profile);
WebDriver driver = new FirefoxDriver(dc);
driver.get("untrusted/self-signed URL");
But issue is the same as above.
Reference Link which have tried :-
How to disable Firefox's untrusted connection warning using Selenium?
https://groups.google.com/forum/?fromgroups#!topic/webdriver/frWtNrEwNPk
Handling UntrustedSSLcertificates using WebDriver
According to this bug Support for untrusted/self-signed certificates has been added via bug 1103196 and will be available starting with Firefox 52.
But I could not find any solution for Firefox v51.0.1 (32-bit).
Is there any way to solve this issue using Firefox v51.0.1 (32-bit)?
As in this bug mentioned Support for untrusted/self-signed certificates will be available starting with Firefox 52, we need to wait until Firefox 52 is not released.
Solution :-
For now, as alternate solution we need to use existing Firefox profile where the certificate for untrusted/self-signed URL is already added into Firefox's exception list.
How to create custom Firefox profile for selenium?
Need to follow this link to create manually custom Firefox profile
Add manually certificate for untrusted/self-signed URL into Firefox's exception list
Launch Firefox using existing profile as :-
System.setProperty("webdriver.gecko.driver", "path/to/geckodriver")
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("created Profile Name");
WebDriver driver = new FirefoxDriver(myprofile);
driver.get("untrusted/self-signed URL");

How to connect to Chromium Headless using Selenium

I would like to use chromium headless for automated testing using selenium. (https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md)
I do have the headless version already running on 9222. So if i open http://10.252.100.33:9222/json/I do get
[ {
"description": "",
"devtoolsFrontendUrl": "/devtools/inspector.html?ws=127.0.0.1:9223/devtools/page/0261be06-1271-485b-bdff-48e443de7a91",
"id": "0261be06-1271-485b-bdff-48e443de7a91",
"title": "The Chromium Projects",
"type": "page",
"url": "https://www.chromium.org/",
"webSocketDebuggerUrl": "ws://127.0.0.1:9223/devtools/page/0261be06-1271-485b-bdff-48e443de7a91"
} ]
As a next step I'd like to connect selenium to the headless chromium. But when i try
final DesiredCapabilities caps = DesiredCapabilities.chrome();
final WebDriver driver = new RemoteWebDriver(new URL("http://localhost:9222/json"), caps);
driver.get("http://www.google.com");
I do get the following logout
Jän 24, 2017 7:14:45 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFORMATION: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
Jän 24, 2017 7:14:45 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFORMATION: Falling back to original OSS JSON Wire Protocol.
Jän 24, 2017 7:14:45 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFORMATION: Falling back to straight W3C remote end connection
org.openqa.selenium.SessionNotCreatedException: Unable to create new remote session. desired capabilities = Capabilities [{browserName=chrome, version=, platform=ANY}], required capabilities = Capabilities [{}]
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700'
System info: host: 'Geralds-MacBook-Pro.local', ip: '192.168.0.249', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12.2', java.version: '1.8.0_111'
Driver info: driver.version: RemoteWebDriver
Questions are:
Is the RemoteWebDriver the correct driver to connect to the headless
chromium?
I read about the DevTool protocol (https://docs.google.com/presentation/d/1gqK9F4lGAY3TZudAtdcxzMQNEE7PcuQrGu83No3l0lw/), but I'm not sure, how to create such a client using selenium.
Connecting the Chromium Headless using the Chrome DevTools works (https://developers.google.com/web/tools/chrome-devtools/remote-debugging/) besides some segmentation vaults ;-)
I think the readme is a little bit misleading. You don't have to start Chromium itself and you can use the RemoteWebDriver. Make sure that a chromedriver is installed (https://sites.google.com/a/chromium.org/chromedriver/home).
Start chromedriver (e.g. ./chromedriver or ./chromedriver --port=9515)
Then you have tell the chromedriver to use Chromium instead of Chrome
Add --headless as an additional argument
Code should look like this:
final ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("/usr/bin/chromium-browser");
chromeOptions.addArguments("--headless");
desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
WebDriver driver = new RemoteWebDriver(url, desiredCapabilities);
Worked for me on Ubuntu Linux.
Alternatively if your running it locally you can just do it like this. In scala.
val chromeOptions = new ChromeOptions
chromeOptions.addArguments("--headless")
new ChromeDriver(chromeOptions)
Use the following code:
ChromeOptions options = new ChromeOptions();
options.setHeadless(true); //Set Chrome option
driver = new ChromeDriver(options);
and you will get "Headless" Chrome!
Full code
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions; //import ChromeOptions
public class web_crawl {
private static WebDriver driver = null;
public static void main(String[] args) {
ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
driver = new ChromeDriver(options);
driver.get("http://www.google.com"); //The website you want to connect to
}
if you are using selenium 3+ chrome driver , you can simply use chrome options and initiate driver. Check details in a project
Example Project on Chrome Headless running with different options
options.setHeadless(true)
System.setProperty("webdriver.chrome.driver", "Path of the chrome driver");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--window-size=1920,1200");
webDriver = new ChromeDriver(chromeOptions);
The invisible browser window is only 800x600 in size. Therefore, you need to set the desired screen size with an additional argument
for me works above solution:
chromeOptions.setBinary("/usr/bin/chromium-browser");
but i had to add (becouse of devtools):
chromeOptions.addArguments("--remote-debugging-port=9222");
and disable firewall
Chrome 59 has the ability to create instance as headless . I have tried for Windows with new chrome driver 2.30 and it worked for me
https://www.automation99.com/2017/07/how-to-use-chrome-headless-using.html?m=1

Categories

Resources