【问题标题】:How can I set a default profile for the Firefox driver in Selenium Webdriver 3?如何在 Selenium Webdriver 3 中为 Firefox 驱动程序设置默认配置文件?
【发布时间】:2019-02-27 02:17:38
【问题描述】:

我无法在 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 version 62.0.2

【问题讨论】:

  • 您应该创建一个 FirefoxOptions 对象来设置配置文件。 FirefoxOptions fo = new FirefoxOptions(); fo.setProfile(profile); WebDriver driver = new FirefoxDriver(fo);
  • 好的,它有效。项目运行没有编译或运行时异常,但是火狐打开了2分钟,为什么要花这么多时间?也许还有其他解决方案。因为没有此配置文件设置,项目将在 10 秒内启动。
  • @VladyslavKuhivchak :理想情况下它应该在 10 秒内开始。你在个人资料中加载了什么?

标签: java selenium firefox geckodriver firefox-profile


【解决方案1】:

将您的设置移至@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();}

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

【讨论】:

    【解决方案2】:

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

    • 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);
    

    解决方案

    要使用默认配置文件调用invoke FirefoxDriver(),您需要使用setProfile(profile) 方法通过FirefoxOptions() 的实例设置FirefoxProfile,您可以使用以下代码块:

    • 代码块:

      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());
            }
      
      }
      
    • 控制台输出:

      [RemoteTestNG] detected TestNG version 6.14.2
      1537775040906   geckodriver INFO    geckodriver 0.20.1
      1537775040923   geckodriver INFO    Listening on 127.0.0.1:28133
      Sep 24, 2018 1:14:30 PM org.openqa.selenium.remote.ProtocolHandshake createSession
      INFO: Detected dialect: W3C
      Google
      PASSED: seleniumFirefox
      
      ===============================================
          Default test
          Tests run: 1, Failures: 0, Skips: 0
      ===============================================
      
      
      ===============================================
      Default suite
      Total tests run: 1, Failures: 0, Skips: 0
      ===============================================
      

    【讨论】:

      猜你喜欢
      • 2012-08-29
      • 2011-01-16
      • 2016-11-08
      • 2021-05-30
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多