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

问题描述 投票:0回答:3

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

java selenium firefox geckodriver firefox-profile
3个回答
3
投票

当您根据 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);

解决方案

要使用

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

2
投票

将您的设置移至

@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();}

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


0
投票

本质上,您传递的是配置文件文件夹,而不是像“默认”这样的配置文件名称

String profileFolder = "..."; // Location according to firefox profile settings 
FirefoxOptions options = new FirefoxOptions(); 
FirefoxProfile profile = new FirefoxProfile(profileFolder);
options.setProfile(profile);
WebDriver driver = new FirefoxDriver(options);

正如评论中已经提到的,设置大约需要 30 秒。

© www.soinside.com 2019 - 2024. All rights reserved.