Selenium Webdriver使用Java自动显示浏览器

时间:2018-09-24 17:29:51

标签: selenium-webdriver

我被写成这段代码来打开Mozilla Firefox浏览器

package com.webdrivercommands;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class WDcommands_Test1 {
public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub

    //Launch chrome Browser
    //setproperty(method)

    System.setProperty("Webdriver.gecko.driver", "C:\\Java Selenium\\geckodriver.exe");

    WebDriver driver=new FirefoxDriver();

    //WebDriver(class) 
    //driver (object)

    //wait Time
    Thread.sleep(5000);

    //close the browser close(method)
    driver.close();
}
}

运行该程序后,出现以下错误..但已经将geckodriver(Firefox驱动程序)提取到我的系统中

  

线程“ main”中的异常java.lang.IllegalStateException:路径   驱动程序可执行文件必须由webdriver.gecko.driver设置   系统属性;有关更多信息,请参见   https://github.com/mozilla/geckodriver。最新版本可以   从https://github.com/mozilla/geckodriver/releases下载于   com.google.common.base.Preconditions.checkState(Preconditions.java:847)     在   org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:125)     在   org.openqa.selenium.firefox.GeckoDriverService.access $ 100(GeckoDriverService.java:43)     在   org.openqa.selenium.firefox.GeckoDriverService $ Builder.findDefaultExecutable(GeckoDriverService.java:168)     在   org.openqa.selenium.remote.service.DriverService $ Builder.build(DriverService.java:346)     在   org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:168)     在   org.openqa.selenium.firefox.FirefoxDriver。(FirefoxDriver.java:125)     在   org.openqa.selenium.firefox.FirefoxDriver。(FirefoxDriver.java:103)     在   com.webdrivercommands.WDcommands_Test1.main(WDcommands_Test1.java:17)

1 个答案:

答案 0 :(得分:0)

示例1:在空白的温度配置文件中启动FF:

    @BeforeClass
    public static void setUpClass() {
        FirefoxOptions options = new FirefoxOptions();
        options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
        System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.19.1-win64\\geckodriver.exe");
        driver = new FirefoxDriver(options);
        driver.manage().window().maximize();}

示例2:在具有某些首选项的空白临时配置文件中启动FF

@BeforeClass
public static void setUpClass() {
    FirefoxOptions options = new FirefoxOptions();  
    FirefoxProfile selenium_profile = new FirefoxProfile();
    selenium_profile.setPreference("browser.download.folderList",2);
    selenium_profile.setPreference("browser.download.dir", "C:\\Users\\pburgr\\Desktop\\BP_usr_tmp\\");
    selenium_profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    options.setProfile(selenium_profile);
    options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
    System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.19.1-win64\\geckodriver.exe");
    driver = new FirefoxDriver(options);
    driver.manage().window().maximize();}

示例3:具有现有FF配置文件的FF:

@BeforeClass
public static void setUpClass() {
    FirefoxOptions options = new FirefoxOptions();
    ProfilesIni allProfiles = new ProfilesIni();         
    FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");
    options.setProfile(selenium_profile);
    options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
    System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.19.1-win64\\geckodriver.exe");
    driver = new FirefoxDriver(options);
    driver.manage().window().maximize();}