Selenium是Firefox的默认配置文件

时间:2018-09-23 09:01:01

标签: java selenium firefox geckodriver firefox-profile

无法在Selenium Webdriver 3中为Firefox设置默认配置文件,因为FirefoxDriver类中没有此类构造函数。

代码:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.ProfilesIni;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class SeleniumStartTest {

  @Test
  public void seleniumFirefox() {
    System.setProperty("webdriver.gecko.driver", "C:\\Users\\FirefoxDriver\\geckodriver.exe");
    ProfilesIni profileIni = new ProfilesIni();
    FirefoxProfile profile = profileIni.getProfile("default");
    WebDriver driver = new FirefoxDriver(profile);
    driver.get("http://www.google.com");
  }
}

Java代码出现编译错误: Java Code

Maven pom.xml依赖项: Selenium 3.14.0

Firefox版本: Firefox version 62.0.2

2 个答案:

答案 0 :(得分:0)

将您的设置移至@BeforeClass

我正在使用此设置,效果很好:

@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.20.0-win64\\geckodriver.exe");
    driver = new FirefoxDriver(options);
    driver.manage().window().maximize();}

只需更改路径和配置文件名称即可。

答案 1 :(得分:0)

根据FirefoxDriver类使用 Selenium 3.14.0 时,有效的构造函数为:

  • FirefoxDriver()
  • FirefoxDriver(FirefoxOptions options)
  • FirefoxDriver(GeckoDriverService service)
  • FirefoxDriver(GeckoDriverService service, FirefoxOptions options)
  • FirefoxDriver(XpiDriverService service)
  • FirefoxDriver(XpiDriverService service, FirefoxOptions options)

因此,按照您的代码尝试,以下不是调用FirefoxDriver()

的有效选项
WebDriver driver = new FirefoxDriver(profile);

解决方案

要使用默认配置文件调用调用FirefoxDriver(),您需要使用setProfile(profile)方法来通过{{ 1}},您可以使用以下代码块:

  • 代码块:

    FirefoxOptions()
  • 控制台输出:

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxOptions;
    import org.openqa.selenium.firefox.FirefoxProfile;
    import org.openqa.selenium.firefox.ProfilesIni;
    import org.testng.annotations.Test;
    
    public class A_FirefoxProfile {
    
          @Test
          public void seleniumFirefox() {
            System.setProperty("webdriver.gecko.driver", "C:/Utility/BrowserDrivers/geckodriver.exe");
            ProfilesIni profileIni = new ProfilesIni();
            FirefoxProfile profile = profileIni.getProfile("default");
            FirefoxOptions options = new FirefoxOptions();
            options.setProfile(profile);
            WebDriver driver = new FirefoxDriver(options);
            driver.get("http://www.google.com");
            System.out.println(driver.getTitle());
          }
    
    }