Как я могу установить профиль по умолчанию для драйвера Firefox в Selenium Webdriver 3?

Я не могу установить профиль по умолчанию для Firefox в Selenium Webdriver 3, потому что такого конструктора нет в классе 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

pom.xml зависимости Maven: Selenium 3.14.0

Версия Firefox: Firefox версии 62.0.2


person Vladyslav Kuhivchak    schedule 23.09.2018    source источник
comment
Вы должны создать объект FirefoxOptions для установки профиля. FirefoxOptions fo = new FirefoxOptions(); fo.setProfile(profile); WebDriver driver = new FirefoxDriver(fo);   -  person skandigraun    schedule 23.09.2018
comment
Хорошо, работает. Проект выполняется без исключений компиляции или времени выполнения, но Firefox открывается уже 2 минуты, зачем столько времени? Может есть другое решение. Потому что без настроек этого профиля проект запускается через 10 сек.   -  person Vladyslav Kuhivchak    schedule 23.09.2018
comment
@VladyslavKuhivchak: В идеале должно начаться через 10 сек. Что все загружаешь в профиль?   -  person cruisepandey    schedule 24.09.2018


Ответы (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), чтобы установить 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
    ===============================================
    
person DebanjanB    schedule 24.09.2018

переместите настройку на @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();}

просто измените пути и обозначение профиля.

person pburgr    schedule 24.09.2018