java - 如何在 Selenium Webdriver 3 中为 Firefox 驱动程序设置默认配置文件?

标签 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 version 62.0.2

最佳答案

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

  • FirefoxDriver()
  • FirefoxDriver(FirefoxOptions 选项)
  • FirefoxDriver(GeckoDriverService 服务)
  • FirefoxDriver(GeckoDriverService 服务,FirefoxOptions 选项)
  • FirefoxDriver(XpiDriverService 服务)
  • FirefoxDriver(XpiDriverService 服务,FirefoxOptions 选项)

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

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

解决方案

要使用 默认配置文件 调用 invoke FirefoxDriver(),您需要使用 setProfile(profile) 方法来设置 FirefoxProfile 通过 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());
          }
    
    }
    
  • 控制台输出:

    [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
    ===============================================
    

关于java - 如何在 Selenium Webdriver 3 中为 Firefox 驱动程序设置默认配置文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52464598/

相关文章:

java - 如何在 DOCX 中使用 POI 从单元格顶部写入

java - Appium:连接到 127.0.0.1:4723 [/127.0.0.1] 失败:连接被拒绝:连接

linux - Linux上的Shell脚本运行java类

css - Firefox 拒绝转换元素

java - 从 java 中清除 IntelliJ 控制台

java - hibernate :无法在类上找到适当的构造函数 - HQL

c# - WebDriverBackedSelenium 可以在 C# 中使用吗

python - 如何在python selenium中上传具有复杂HTML情况的图片

css - Opera 和 Firefox 的正确边框渐变语法,我的 webkit 语法工作正常

html - 是否可以使用 CSS3 更改列表项的顺序?