Selenium 3.0.1 with new FirefoxDriver + Marionette stuck on start page - java

I'm migrating my Selenium app from 2.53 to 3.0.1. First of all, I want to test it in a small app just launching a browser and navigating to a specific page.
Moreover, I want to use a custom binary for Firefox (version 51.0, Portable App).
This is my code:
public class Selenium {
public static void main(String[] args) {
WebDriver driver = createFFDriver();
driver.navigate().to("http:....");
System.out.println("Finished");
}
public static WebDriver createFFDriver(){
System.setProperty(GeckoDriverService.GECKO_DRIVER_EXE_PROPERTY,"foo/geckodriver64.exe");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("firefox_binary","foo/firefox.exe");
return new FirefoxDriver(capabilities);
}
}
The browser actually opens up, but blocked.
Logs:
1486713046153 geckodriver INFO Listening on 127.0.0.1:12466
Feb 10, 2017 8:50:46 AM org.openqa.selenium.remote.ProtocolHandshake
createSession INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
1486713046731 mozprofile::profile INFO Using profile path foo\AppData\Local\Temp\rust_mozprofile.p25D0Gb1sBQm
1486713046752 geckodriver::marionette INFO Starting browser foo\firefox\51.0\FirefoxPortable.exe
1486713046782 geckodriver::marionette INFO Connecting to Marionette on localhost:52818
Why is Geckodriver listening on 127.0.0.1:12466 but it's trying to connect to Marionette on localhost:52818 ?
This is the page where I get stuck:
EDIT:
It gets stuck in the RemoteWebDriver startSession method:
Response response = this.execute("newSession", parameters);

I have tried your code with Selenium V 3.0.1 and Firefox 51.0.1 (32-bit) and was successful in accessing URL/other web driver functions/no blocking.
UPDATE
WebDriver firefox;
System.setProperty(GeckoDriverService.GECKO_DRIVER_EXE_PROPERTY,"pathtogeckodriver");
FirefoxProfile profile = new FirefoxProfile();
firefox = new FirefoxDriver( new FirefoxBinary(
new File(System.getProperty("user.dir"),
"\\FirefoxPortable\\FirefoxPortable.exe")), profile);
driver.get("http://www.google.com");
This works perfectly with Portable Firefox version (51.0.1)

It seems that the Portable Edition of Firefox isn't working well with Gecko Driver.
It works as expected with traditional Firefox (51.0.1).

Related

Selenium: Unable to Locate Browser Binaries on Linux

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

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 GeckoDriver get IP and Port no of the launched driver instance

I'm using Selenium 3.4, Geckodriver 0.17.
I launch FirefoxDriver using the below code
System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://www.bing.com");
System.out.println(driver.getSessionId());
Is there a way I can get the IP and the port of the launched driver instance?
The data I want is printed in the logs.
1499170600204 geckodriver INFO Listening on 127.0.0.1:38840
1499170601127 geckodriver::marionette INFO Starting browser C:\Program Files\Mozilla Firefox\firefox.exe with args ["-marionette"]
[GFX1]: Potential driver version mismatch ignored due to missing DLLs igd10umd32 v= and igd10iumd32 v=
1499170608388 Marionette INFO Listening on port 12793
Jul 04, 2017 5:46:48 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
The First line of the output 127.0.0.1:38840 prints the info I want. I don't want to parse the log as I will be running drivers in parallel.
The RemoteWebDriver has getCommandExecutor method.
Which can be TypeCasted to HttpCommandExecutor and getAddressOfRemoteServer() method returns the URL.
HttpCommandExecutor ce = (HttpCommandExecutor) driver.getCommandExecutor();
System.out.println(ce.getAddressOfRemoteServer());

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