如何通过GeckoDriver和Selenium启动Firefox浏览器

时间:2018-10-09 07:03:42

标签: java selenium firefox selenium-webdriver geckodriver

我正在尝试启动Selenium Web浏览器,我能够打开Firefox浏览器,但无法打开google来解决我遇到的此错误

package selenium1;

import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class selenium2 {
    public static void main(String[] args) throws Exception {

        System.out.println("hello world ");
        System.setProperty.("webdriver.firefox.marionette","/Users/bindumalini.n/Downloads/geckodriver.exe";
        WebDriver driver = new FirefoxDriver();
        driver.quit();
        driver.get("www.gmail.com");
        System.out.println("hello");
    }
}

输出//

  

你好世界   线程“主”中的异常org.openqa.selenium.WebDriverException:等待45秒,等待Firefox启动超时。   生成信息:版本:'3.14.0',修订版本:'aacccce0',时间:'2018-08-02T20:05:20.749Z'   系统信息:主机:'BLRJPT6861D',ip:'fe80:0:0:0:148a:bc94:9bd4:8a04%en0',操作系统名称:'Mac OS X',os.arch:'x86_64',os .version:“ 10.13.6”,java.version:“ 1.8.0_141”   驱动程序信息:driver.version:FirefoxDriver       在org.openqa.selenium.firefox.XpiDriverService.waitUntilAvailable(XpiDriverService.java:133)       在org.openqa.selenium.firefox.XpiDriverService.start(XpiDriverService.java:118)       在org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:79)       在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)       在org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:212)       在org.openqa.selenium.remote.RemoteWebDriver。(RemoteWebDriver.java:130)       在org.openqa.selenium.firefox.FirefoxDriver。(FirefoxDriver.java:125)       在org.openqa.selenium.firefox.FirefoxDriver。(FirefoxDriver.java:103)       在selenium1.selenium2.main(selenium2.java:13)   由以下原因引起:org.openqa.selenium.net.UrlChecker $ TimeoutException:等待[http://localhost:48007/hub/status]可用在45005 ms之后超时       在org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:100)       在org.openqa.selenium.firefox.XpiDriverService.waitUntilAvailable(XpiDriverService.java:131)       ...另外8个   造成原因:java.util.concurrent.TimeoutException       在java.util.concurrent.FutureTask.get(FutureTask.java:205)       在com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:156)       在org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:75)       ...还有9个

3 个答案:

答案 0 :(得分:0)

您在导航之前使用了driver.quit();,这会退出该驱动程序并关闭关联的窗口。

将其移至脚本末尾(或最后一次使用后)

public static void main(String[] args) throws Exception {
    System.out.println("hello world ");
    System.setProperty.("webdriver.firefox.marionette","/Users/bindumalini.n/Downloads/geckodriver.exe";
    WebDriver driver = new FirefoxDriver();     
    driver.get("www.gmail.com");
    driver.quit();
    System.out.println("hello");
}

答案 1 :(得分:0)

在使用 Selenium v​​3.14.0 而不是webdriver.firefox.marionette时,需要强制使用 GeckoDriver 作为 webdriver.gecko.driver >。

此外,就像在 Mac OS X 上一样,您必须从mozilla/geckodriver下载 geckodriver-v0.23.0-macos.tar.gz ,同时指定删除扩展名所需的绝对路径(即.exe

您可以使用以下解决方案:

package selenium1;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class selenium2 {
    public static void main(String[] args) throws Exception {

    System.out.println("hello world ");
    System.setProperty.("webdriver.gecko.driver","/Users/bindumalini.n/Downloads/geckodriver");
    WebDriver driver = new FirefoxDriver();
    driver.get("www.gmail.com");
    System.out.println("hello");
    driver.quit();
    }
}

答案 2 :(得分:0)

尝试这个希望对您有所帮助。这是一个浏览器动态硒代码... 如果仍然遇到任何问题,请确认。

打包com.selenium;

导入org.openqa.selenium.WebDriver;

导入org.openqa.selenium.chrome.ChromeDriver;

导入org.openqa.selenium.edge.EdgeDriver;

导入org.openqa.selenium.firefox.FirefoxDriver;

导入org.openqa.selenium.ie.InternetExplorerDriver;

导入org.openqa.selenium.opera.OperaDriver;

导入org.testng.annotations.Test;

/ **  * @作者mohitjaiswal  *  * /

公共类BrowserDynamically {

WebDriver driver = null;
String browser = "chrome";

(在此处传递浏览器名称,例如“ chrome”,“ firefox”,“ edgeBrowser”,“ opera”,“ IE”,以字符串形式:要打开浏览器)

String edgePath = "/Users/mohitjaiswal/Documents/\"\n" + 
                    "+ \"My-Data/Selenium Libraries/Drivers/MicrosoftWebDriver.exe";
String chromePath = "/Users/mohitjaiswal/Documents/"
        + "My-Data/Selenium Libraries/Drivers/chromedriver";
String firefoxPath = "/Users/mohitjaiswal/Documents/"
        + "My-Data/Selenium Libraries/Drivers/geckodriver";
String opraPath = "/Users/mohitjaiswal/Documents/"
        + "My-Data/Selenium Libraries/Drivers/operadriver";
String iePath = "/Users/mohitjaiswal/Documents/"
        + "My-Data/Selenium Libraries/Drivers/IEDriverServer.exe";


public void invokeBrowser(String browserName) {

    if(browserName.equalsIgnoreCase("chrome")) {
        System.setProperty("webdriver.chrome.driver", chromePath);
        driver = new ChromeDriver();
    }else if (browserName.equalsIgnoreCase("firefox")) {
        System.setProperty("webdriver.gecko.driver", firefoxPath);
        driver = new FirefoxDriver();
    }else if (browserName.equalsIgnoreCase("edgeBrowser")) {
        System.setProperty("webdriver.edge.driver", edgePath);
        driver = new EdgeDriver();
    }else if (browserName.equalsIgnoreCase("opera")) {
        System.setProperty("webdriver.opera.driver", opraPath);
        driver = new OperaDriver();
    }else if (browserName.equalsIgnoreCase("IE")) {
        System.setProperty("webdriver.ie.driver", iePath);
        driver = new InternetExplorerDriver();
    }
}

@Test
public void invokeApplication() {
    invokeBrowser(browser);
    driver.get("https://www.google.com");
    driver.manage().window().maximize();
    driver.close();
}

}