org.openqa.selenium.SessionNotCreatedException:通过Selenium v​​3.11.0

时间:2018-04-12 06:58:40

标签: selenium firefox selenium-webdriver webdriver geckodriver

我正在尝试在Firefox中运行测试运行网站,但我收到错误"驱动程序可执行文件的路径必须由webdriver.gecko.driver系统属性设置;"我已经正确设置了路径,但仍然不知道问题出在哪里。这是我的代码:

if (browsers.equalsIgnoreCase("Firefox")) 
{
        String driverPath = System.getProperty("user.dir") + "\\src/test/java\\drivers\\geckodriver.exe";
        System.setProperty("webdriver.firefox.marionette", "false");
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    } else if (browsers.equalsIgnoreCase("Chrome")) {
        // String driverPath = System.getProperty("user.dir") +
        // "\\src\\Drivers\\chromedriver";
        // System.setProperty("webdriver.chrome.driver", driverPath);
        System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    }

以下是我的代码的失败追踪:

FAILED CONFIGURATION: @BeforeClass launchBrowser
org.openqa.selenium.SessionNotCreatedException: Unable to find a matching set of capabilities
Build info: version: '3.8.0', revision: '924c4067df', time: '2017-11-30T11:36:59.109Z'
System info: host: 'DSCHD-PC-180', ip: '192.168.6.122', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_161'
Driver info: driver.version: FirefoxDriver
remote stacktrace: 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$new$0(W3CHandshakeResponse.java:57)
    at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$getResponseFunction$2(W3CHandshakeResponse.java:104)
    at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:123)
    org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:126)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:73)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:219)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:142)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:120)
    at Utility.UtilityMethods.openBrowser(UtilityMethods.java:26)
    at TestCases.LoginTestCase.launchBrowser(LoginTestCase.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:455)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142)
    at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:168)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:105)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

SKIPPED CONFIGURATION: @AfterClass quitBrowser
SKIPPED: registration   

5 个答案:

答案 0 :(得分:1)

这是系统属性的设置方式:

System.setProperty("webdriver.gecko.driver", "C:\\...\\geckodriver.exe");

答案 1 :(得分:0)

您正在使用 user.dir ,这基本上意味着:当前工作目录

这是允许您使用selenium初始化firefox浏览器的代码。

public static void main(String[] args) {


            System.setProperty("webdriver.gecko.driver", "C:\\your\\gecko\\driver\\path\\geckodriver.exe");
            driver = new firefoxDriver();

那就是它!您可以使用Firefox驱动程序执行以下操作:

1.  driver.get(String args0);
2.  driver.getCurrentUrl();
3.  driver.getPageSource();
4.  driver.getTitle();
5.  driver.findElememt(By args0);
6.  driver.findElements(By args0);
7.  driver.naviagate();
8.  driver.manage();
9.  driver.close();
10. driver.quit();
11. driver.switchTo();
12. driver.getWindowHandle();
13. driver.getWindowHandles();

如果您对此有任何疑虑,请与我们联系。

答案 2 :(得分:0)

刚刚更新了您的代码。您必须为setProperty和用户webdriver.gecko.driver设置最新firefox驱动程序的驱动程序路径。

    if (browsers.equalsIgnoreCase("Firefox")) {
            String driverPath = System.getProperty("user.dir") + "\\src\\test\\java\\drivers\\geckodriver.exe"; 
            System.setProperty("webdriver.firefox.driver", "driverPath"); 
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability("marionette", false); 
            FirefoxOptions options = new FirefoxOptions(); 
            options.merge(capabilities);
            FirefoxDriver driver = new FirefoxDriver(options);
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        } 
        else if (browsers.equalsIgnoreCase("Chrome")) {
            // String driverPath = System.getProperty("user.dir") +
            // "\\src\\Drivers\\chromedriver";
            // System.setProperty("webdriver.chrome.driver", driverPath);
            System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
            driver = new ChromeDriver();
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        }

让我知道。

答案 3 :(得分:0)

为了简单起见,当您使用 Selenium Client v3.11.0 Firefox v37 时,您需要从{下载最新的 GeckoDriver {3}}并将其保存在系统中的任何位置。在System.setProperty()行中的下一行将 webdriver.gecko.driver 一起传递为绝对路径< GeckoDriver 的/ em>最后通过 DesiredCapabilities 类将 marionatte 的功能设置为 false 并合并到一个实例中 FirefoxOptions 实例启动 Firefox 浏览器,如下所示:

System.setProperty("webdriver.gecko.driver", "C:/path/to/geckodriver.exe");
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability("marionatte", false);
FirefoxOptions opt = new FirefoxOptions();
opt.merge(dc);
FirefoxDriver driver =  new FirefoxDriver(opt);
driver.get("https://stackoverflow.com");
System.out.println("Application opened");
System.out.println("Page Title is : "+driver.getTitle());
driver.quit();

更新

根据您的评论更新,您使用的是 GeckoDriver v0.20.1

但是,如果你回顾一下 GeckoDriver v0.18.0 的发行说明,它会清楚地提到:

geckodriver now recommends Firefox 53 and greater

因此,使用 GeckoDriver v0.18.0 及更高版本,您必须强制使用Firefox 53及更高版本。要摆脱这种约束,您可以降级到以下任何一个版本:

  • GeckoDriver v0.17.0
  • GeckoDriver v0.16.1

答案 4 :(得分:0)

我有此错误2天,对我来说,解决方案是在Set.Plataform中放置Platafor.ANY或Plataform.Windows,因为Plataform.WIN10不起作用,不需要木偶,并且我添加并且始终起作用,这仅适用。我希望这可以帮助其他人:

public class Main { public static RemoteWebDriver driver;

public static void main(String[] args) throws MalformedURLException {
    System.setProperty("webdriver.gecko.driver", "D:/Lib/geckodriver.exe");
    DesiredCapabilities desiredCapabilities = new DesiredCapabilities().firefox();
    desiredCapabilities.setPlatform(Platform.ANY);
    desiredCapabilities.setBrowserName("firefox");

    driver = new RemoteWebDriver(new URL("http://172.20.19.182:5557/wd/hub"), desiredCapabilities);
    driver.navigate().to("http://www.google.com");
    driver.findElementByName("q").sendKeys("execute automation");
    driver.findElementByName("q").sendKeys(Keys.ENTER);
    driver.close();
    // write your code here
}