一尘不染

org.openqa.selenium.remote.UnreachableBrowserException:无法启动新的会话。可能的原因是远程服务器的地址无效

selenium

我的selenium代码未运行。不断向我抛出以下错误:

    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版本:53.0.3(32位)SELENIUM版本:selenium-java-3.4.0我正在使用Eclipse
Luna,并且我的机器是Windows 7(64位)。我已经阅读了所有帮助查询,但找不到解决方案。这是我的Java代码:

    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);
    }
    }

阅读 563

收藏
2020-06-26

共1个答案

一尘不染

您必须在代码中考虑几个因素,如下所示:

  1. 您已通过创建的相同类的对象PractiseSession1 ade= new PractiseSession1();,并使用对象ade来调用不同的方法OpenBrowser()GetPage()quitbrowser()。这些方法执行的功能可以通过其中的一行代码来实现main(),而无需创建任何对象。
  2. 在遵循 W3C Standards的情况 下使用Selenium 3.x时,要与 geckodriver.exe 一起使用webdriver.gecko.driver,我们需要使用而不是webdriver.firefox.marionetteSystem.setProperty一行中。
  3. 在提及时,System.setProperty您需要提供 geckodriver.exe 的绝对路径,如下所示:

    System.setProperty("webdriver.gecko.driver", "C:\\your_directory\\geckodriver.exe");
    
  4. 一旦提及ImplicitlyWait,它将在程序执行期间保留下来。您可以考虑删除多个提及。

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

  1. 您的整个代码可以仅用6行编写,如下所示:
        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");
    }

    }   
2020-06-26