当前位置: 首页 > 面试题库 >

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

郎志
2023-03-14
问题内容

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

Maven pom.xml依赖项: Selenium 3.14.0

Firefox版本: Firefox 62.0.2版


问题答案:

当您按照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)方法通过的实例设置
FirefoxProfileFirefoxOptions()并且可以使用以下代码块:

  • 代码块:

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



 类似资料:
  • 如果未设置,我希望默认活动配置文件为。 Spring-boot版本=1.3.5。发行版

  • 当我给出命令时,我得到默认配置文件的以下输出: 然而,当我给一个命名的配置文件的命令时,我得到了一个配置文件名 我曾尝试通过读取'set'CLI命令将默认配置文件命名为,我还尝试了,因为我认为在阅读本页后,变量的命名就是这样的。我之所以要这样做,是因为我想要有两个配置文件,并且我想要使用环境变量在配置文件之间切换。

  • 因此,我正在python上创建一个使用selenium的机器人,在这种情况下,我想将我的代码从windows迁移到linux;例如,我想使用chrome的默认配置文件;因此,我不必设置会话和其他变量: options.add_argument("user-data-dir=C:\User\myUser\AppData\Local\Google\Chrome\User Data") 这适用于wind

  • 问题内容: 我有一个应用程序,我想在其中定义要为连接预取的默认行数,同时用于Oracle和SQL Server驱动程序。Oracle驱动程序具有一个接口,该接口提供了执行该操作的方法,但是我没有找到与SQL Server驱动程序等效的任何东西。 有没有一种方法可以使用SQL Server JDBC驱动程序为连接定义默认的行预取​​? 问题答案: 设置行提取大小的常用方法是: 通过 供应商实施类的

  • 问题内容: 我发现在hibernate配置文件中,我们可以设置参数: 现在,我正在使用JPA,我也想这样做。否则,我必须将参数添加到每个@Table注释中,例如: 据我了解,此参数应该在配置的这一部分中: …但是我在Google中找不到它的名字。有任何想法吗? 问题答案: 也不知道为此的JPA属性。但是,您可以仅将Hibernate属性(假设您使用Hibernate作为提供程序)添加为 hiber

  • 问题内容: 我使用Selenium 2.35.0并配置代理设置,例如: 当我需要更改代理设置时,我会强制重新启动Webdriver并指定其他“ proxyStr”。 如何在不重新启动Webdriver的情况下实现此更改? 问题答案: 为任何给定的驱动程序设置代理时,仅在创建WebDriver会话时设置代理;它不能在运行时更改。即使您具有创建的会话的功能,也将无法更改它。因此,答案是,不,如果要使用