Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium webdriver Selenium:UnreachableBrowserException显示在带有testNG的chromeDriver中_Selenium Webdriver_Selenium Chromedriver - Fatal编程技术网

Selenium webdriver Selenium:UnreachableBrowserException显示在带有testNG的chromeDriver中

Selenium webdriver Selenium:UnreachableBrowserException显示在带有testNG的chromeDriver中,selenium-webdriver,selenium-chromedriver,Selenium Webdriver,Selenium Chromedriver,当我运行以下代码时,下面的错误显示为:org.openqa.selenium.remote.UnreachableBrowserException:无法启动新会话。可能的原因是远程服务器地址无效或浏览器启动失败 chrome浏览器尚未启动 //baseClass.java: 公共类基类{ //ThreadLocal will keep local copy of driver public static ThreadLocal<RemoteWebDriver> dr = new Th

当我运行以下代码时,下面的错误显示为:org.openqa.selenium.remote.UnreachableBrowserException:无法启动新会话。可能的原因是远程服务器地址无效或浏览器启动失败

chrome浏览器尚未启动

//baseClass.java:

公共类基类{

//ThreadLocal will keep local copy of driver
public static ThreadLocal<RemoteWebDriver> dr = new ThreadLocal<RemoteWebDriver>();

@BeforeTest
//Parameter will get browser from testng.xml on which browser test to run
@Parameters("myBrowser")
public void beforeClass(String myBrowser) throws MalformedURLException{
    try {
        RemoteWebDriver driver = null;

        if(myBrowser.equals("chrome")){
            DesiredCapabilities capability = new DesiredCapabilities().chrome();
            capability.setBrowserName("chrome");
            capability.setPlatform(Platform.WINDOWS);
            driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
        }
        else if(myBrowser.equals("firefox")){
            DesiredCapabilities capability = new DesiredCapabilities().firefox();
            capability.setBrowserName("firefox");
            capability.setPlatform(Platform.WINDOWS);
            driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
        }

        //setting webdriver
        setWebDriver(driver);

        getDriver().manage().window().maximize();
        getDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }catch (Exception ex){
        System.out.println(ex.toString());
    }
}

public WebDriver getDriver() {
    return dr.get();
}

public void setWebDriver(RemoteWebDriver driver) {
    dr.set(driver);
}

@AfterClass
public void afterClass(){
    getDriver().quit();
    dr.set(null);

}

}

在初始化RemoteWebDriver之前,必须为chrome/gecko驱动程序设置系统属性。大概

 if(myBrowser.equals("chrome")){
        DesiredCapabilities capability = new DesiredCapabilities().chrome();
        capability.setBrowserName("chrome");
        capability.setPlatform(Platform.WINDOWS);
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe"); 
        driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
    }
    else if(myBrowser.equals("firefox")){
        DesiredCapabilities capability = new DesiredCapabilities().firefox();
        capability.setBrowserName("firefox");
        capability.setPlatform(Platform.WINDOWS);
        System.setProperty("webdriver.gecko.driver", "C:\\geckodriver\\geckodriver.exe"); 
        driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
    }
可能重复的