firefox selenium web驱动出错

Fen*_*ici 1 java firefox selenium selenium-webdriver

最近,在使用 selenium firefox 驱动程序时遇到了这个问题。提前致谢

设置

os.name: 'Mac OS X',

os.arch: 'x86_64',

os.version: '10.12.6',

java.version: '1.8.0_131'

Firefox 版本56.0.1(64 位)

Gecko 驱动程序 最新 0.19.0

错误显示失败:org.openqa.selenium.SessionNotCreatedException:试图在没有建立连接的情况下运行命令

我尝试了不同的方法来解决它,但总是出现相同的错误。1.更新所有selenium测试驱动到最新2.指定目录export PATH = $PATH driverDir

我的代码

  package automationFramework;

import org.apache.commons.io.FileUtils;
import org.junit.*;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.safari.SafariDriver;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;

public class GeckoDriver {
    private static WebDriver driver;
    public static int random = 0;
    private String baseURL;
    // @BeforeClass : Executes only once for the Test-Class.
    @BeforeClass
    public static void setting_SystemProperties(){
        System.out.println("System Properties seting Key value.");

    }
    // @Before      : To execute once before ever Test.
    @Before
    public void test_Setup(){
        System.out.println("Launching Browser");
        if (random == 0) {
            System.out.println("Start Chrome Browser Testing ");
            System.setProperty("webdriver.gecko.driver", "/Users/Fannity/Desktop/Drivers/geckodriver");  // Chrome Driver Location.
            driver = new FirefoxDriver();
        }

        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        System.out.println("Session ID : " + ((RemoteWebDriver) driver).getSessionId() );
    }

    @Test
    public void selenium_ScreenShot() throws IOException {
        baseURL = "https://www.google.com/";
        driver.get(baseURL);
        System.out.println("Selenium Screen shot.");
        File screenshotFile = ((RemoteWebDriver) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshotFile, new File("/Users/Fannity/Desktop/JUNIT-Selenium.jpg"));
        random += 1;
    }

    // @After       : To execute once after ever Test.
    @After
    public void test_Cleaning(){
        System.out.println("Closing Browser");
        baseURL = null;
        driver.close();
        driver.quit();
    }
    // @AfterClass  : Executes only once before Terminating the Test-Class.
    @AfterClass
    public static void clearing_SystemProperties(){
        System.out.println("System Property Removing Key value.");
        System.clearProperty("webdriver.gecko.driver");
    }
}
Run Code Online (Sandbox Code Playgroud)

错误

https://gist.github.com/Fenici/f82f885486de37ae110fda8d7430df6e

Dav*_*tti 5

你的问题在这里:

@After
    public void test_Cleaning(){
        System.out.println("Closing Browser");
        baseURL = null;
        driver.close();
        driver.quit();
    }
Run Code Online (Sandbox Code Playgroud)

仅尝试使用close(). 这里解释的很好。