本文介绍了如何在Selenium Webdriver 3中为Firefox驱动程序设置默认配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can't set a default profile for Firefox in Selenium Webdriver 3 because there is no such constructor in the FirefoxDriver class.

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代码

Compile error in Java code:Java Code

Maven pom.xml依赖项:硒3.14.0

Maven pom.xml dependencies:Selenium 3.14.0

Firefox版本: Firefox 62.0.2版

Firefox version:Firefox version 62.0.2

推荐答案

根据 FirefoxDriver 类的有效构造函数为:

As you are using Selenium 3.14.0 as per the FirefoxDriver Class the valid constructors are:

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

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

So, as per your code attempts the following is not a valid option to invoke FirefoxDriver()

WebDriver driver = new FirefoxDriver(profile);

解决方案

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

Solution

To invoke invoke FirefoxDriver() with the default profile you need to use the setProfile(profile) method to set the FirefoxProfile through an instance of FirefoxOptions() and you can use the following code block:

  • 代码块:

  • Code Block:

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

}

  • 控制台输出:

  • Console Output:

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

  • 这篇关于如何在Selenium Webdriver 3中为Firefox驱动程序设置默认配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-19 02:39