【问题标题】:Java throws Exception :Selenium 3.4.0,Gecko driver 16.01,FF 53.01Java 抛出异常:Selenium 3.4.0,Gecko 驱动程序 16.01,FF 53.01
【发布时间】:2017-06-04 05:39:26
【问题描述】:

运行时,java抛出以下异常: 线程“主”org.openqa.selenium.SessionNotCreatedException 中的异常:无法创建新的远程会话。所需功能 = 功能 [{marionette=true, browserName=firefox, version=, platform=A。 我正在使用 gecko 驱动程序 16.1。当我使用 gecko 14.01 时,它会导航到 gmail 页面,然后即使我设置了隐式等待也无法找到元素。

    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.MarionetteDriver;
    import org.openqa.selenium.remote.DesiredCapabilities;
    public class login {
    public static void main(String[] args) {
        // Create a new instance of the Firefox driver
    System.setProperty("webdriver.gecko.driver","C:/Users/asdf/Desktop/selenium/gecko32/geckodriver.exe");
    DesiredCapabilities capabilities=DesiredCapabilities.firefox();
    capabilities.setCapability("marionette", true);
    WebDriver driver = new MarionetteDriver(capabilities);
        //WebDriver driver = new FirefoxDriver();
        //  Wait For Page To Load
        // Put a Implicit wait, this means that any search for elements on the page
    driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        // Navigate to URL
    driver.get("https://mail.google.com/");
    driver.manage().window().maximize();
    //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        // gmail login
    driver.findElement(By.xpath("//*[@id='identifierId']")).sendKeys("username");
    driver.findElement(By.id("next")).click();
    driver.findElement(By.id("Passwd")).sendKeys("password");

【问题讨论】:

    标签: java selenium firefox


    【解决方案1】:

    这是您问题的答案:

    SessionNotCreatedException 可以观察到不同的原因,例如 Selenium-geckodriver 版本不匹配、悬空 geckodriver 实例和 OS chores。我建议您考虑以下步骤:

    1. Task Manager 手动/以编程方式杀死所有悬空的geckodriver 实例。如果可能,重新启动您的系统。如果需要,请运行 CCleaner 以清除系统中所有不需要的操作系统杂项。
    2. 从此page下载Selenium Standalone Server 3.4.0Selenium Client 3.4.0
    3. 从此page下载geckodriver v.0.16.1
    4. 确保您已安装Mozilla Firefox 53.0 的最新稳定GA 版本
    5. MarionetteDriver 实现已停止,您可以考虑使用FirefoxDriver 实现。
    6. 有关MarionetteDriverGeckoDriver 的详细讨论,您可以考虑查看this discussion
    7. 这是您自己的代码,经过一些小调整后运行良好:

      package demo;
      
      import java.util.concurrent.TimeUnit;
      
      import org.openqa.selenium.By;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.firefox.FirefoxDriver;
      import org.openqa.selenium.remote.DesiredCapabilities;
      
      public class Q44351100_SessionNotCreatedException 
      {
          public static void main(String[] args) 
          {
              System.setProperty("webdriver.gecko.driver","C:/your_directory/geckodriver.exe");
              DesiredCapabilities capabilities=DesiredCapabilities.firefox();
              capabilities.setCapability("marionette", true);
              WebDriver driver = new FirefoxDriver(capabilities);
              driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
              driver.get("https://mail.google.com/");
              driver.manage().window().maximize();
              driver.findElement(By.xpath("//*[@id='identifierId']")).sendKeys("username");
          }
      }
      

    如果这能回答您的问题,请告诉我。

    【讨论】:

    • 嗨!非常感谢详细的解决方案。我已经完成了所有工作,我尝试运行您的代码。它导航到 gmail 页面,但无法使用发送密钥并将用户名发送到字段。
    【解决方案2】:

    尝试使用最新版本的 Firefox 浏览器和 gecko 驱动程序。希望这会有所帮助

    【讨论】:

    • 正如我所提到的,我使用的是最新的 Firefox 53.0 和最新的壁虎 16.01
    • 使用 geckodriver v0.16.1
    • 还将您的 selenium 版本升级到 3.4.0
    • 是的,正如我在标题中提到的,我使用的是最新的
    • 尝试使用 firefoxdriver 代替 marionette 驱动程序,但也没有能力
    【解决方案3】:

    启动 FirefoxDriver 而不是 Marionette 驱动程序。

    WebDriver driver = new FirefoxDriver(capabilities);
    

    希望这对您有所帮助。谢谢。

    【讨论】:

    • 我照你说的做了。我得到了这个异常:线程“main”org.openqa.selenium.SessionNotCreatedException中的异常:无法创建新的远程会话。所需功能 = 功能 [{marionette=true, firefoxOptions=org.openqa.selenium.firefox.FirefoxOptions@545997b1, browserName=firefox, moz:firefoxOptions=org.openqa.selenium.firefox.FirefoxOptions@545997b1,
    • 你有没有收到任何错误信息...... WebDriver driver = new FirefoxDriver();;
    • 是的,同样的错误。正如每个人都建议我使用最新的 selenium 和 gecko v0.16.01。但 gecho 14 一直工作到登录页面。但它不能使用 sendkeys
    • 它在我的机器上工作。我确信我们需要使用 firefox 驱动程序而不是 marionetter。我正在使用 selenium 3.2、firefox 13 和 gecko 0.14。
    • 我正在使用 FF 53、selenium 3.4.0 和 gecko v 0.16.1。我也尝试了你的 seuugestion,仍然显示相同的错误。我使用 eclipse kepler 和 jdk 1.8。代码上没有看到错误
    猜你喜欢
    • 2017-06-03
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    相关资源
    最近更新 更多