My selenium code does not run. Keeps throwing me the following error:
Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'SLAP129', ip: '192.168.4.218', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_74'
Driver info: driver.version: FirefoxDriver
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:658)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:250)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:137)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:191)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:108)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:104)
at PractiseSession1.OpenBrowser(PractiseSession1.java:35)
at PractiseSession1.main(PractiseSession1.java:16)
Caused by: java.lang.NullPointerException
at org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:192)
at org.openqa.selenium.firefox.XpiDriverService.start(XpiDriverService.java:94)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:78)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
... 8 more
FIREFOX VERSION: 53.0.3 (32 Bit) SELENIUM VERSION: selenium-java-3.4.0 I am using Eclipse Luna and my machine is Windows 7 (64 bit). I have read all the the help queries but could not find solution. Here is my Java code:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class PractiseSession1
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
WebDriver driver = null;
String URL="http://www.google.com";
//System.out.println("Application title is =============");
PractiseSession1 ade= new PractiseSession1();
ade.OpenBrowser(driver);
ade.GetPage(URL, driver);
ade.quitbrowser(driver);
}
private void quitbrowser(WebDriver driver)
{
driver.quit();
}
private void GetPage(String URL, WebDriver driver)
{
driver.get(URL);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
private void OpenBrowser(WebDriver driver)
{
System.setProperty("webdriver.firefox.marionette", "<Geckodriver
path>geckodriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
driver= new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
}
You have to consider a couple of factors in your code as follows:
- You have created an object of the same class through
PractiseSession1 ade= new PractiseSession1();
and using the objectade
to call the different methodsOpenBrowser()
,GetPage()
andquitbrowser()
. The functionality performed by the methods can be achieved through a single line of code withinmain()
and that too without creating any object. - While using Selenium 3.x following the W3C Standards, to work with geckodriver.exe we need to use
webdriver.gecko.driver
instead ofwebdriver.firefox.marionette
in theSystem.setProperty
line. While you mention
System.setProperty
you need to provide the absolute path of the geckodriver.exe as follows:System.setProperty("webdriver.gecko.driver", "C:\\your_directory\\geckodriver.exe");
Once you mention
ImplicitlyWait
, it is retained throughout the execution of your program. You may consider removing the multiple mentions.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Your entire code can be written in just 6 lines as follows:
package demo; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.remote.DesiredCapabilities; public class Q44308973_remote_unreachablebrowserexception { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\\your_directory\\geckodriver.exe"); DesiredCapabilities dc = DesiredCapabilities.firefox(); dc.setCapability("marionette", true); WebDriver driver = new FirefoxDriver(dc); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://google.com"); } }
For a detailed understanding of how webdriver.firefox.marionette
evolved to be webdriver.gecko.driver
you can watch this space.
You should replace
driver = new FirefoxDriver();
with
driver = new FirefoxDriver(capabilities);
so that you run the test with the desired capabilities.
Only issue with that is that it may not work with 3.4.0 as the default timeout value was reduce and may be too short now.
来源:https://stackoverflow.com/questions/44308973/org-openqa-selenium-remote-unreachablebrowserexception-could-not-start-a-new-se