Search code examples
javaseleniumauthenticationfirefoxpopupwindow

Authentication popup goes into the background Selenium, Firefox


I have had the problem for 2 weeks that when I create a new Firefox driver in Selenium, the authentication popup for the proxy is immediately pushed into the background. Selenium can't reach it there anymore. Do you have a solution to the problem? I am using Selenium 3.141.5, Java 1.8. and Firefox version 63.0.1.

System.setProperty("webdriver.gecko.driver", "C:\\Program Files\\geckodriver.exe");
FirefoxOptions options = new FirefoxOptions();
options.setBinary("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver(options);
try {
        Alert alert = driver.switchTo().alert();
        alert.sendKeys("Username" + Keys.TAB + "Password");
        alert.accept();
        driver.switchTo().defaultContent();
}catch (NoAlertPresentException e) {
        e.printStackTrace();
}
driver.get("https://www.google.de/");

EDIT: I tested it with Firefox version 62.0.3, everything works there.


Solution

  • Best way is to avoid the pop-up.

    1. hit Win+R, run "firefox -p" and create new profile (let's call it selenium_profile)
    2. run Firefox in selenium_profile, login to the proxy and save your credetials to Firefox
    3. use the customized profile, there is my setup:

      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", sec_var.driver_path);
      driver = new FirefoxDriver(options);
      driver.manage().window().maximize();
      

    With custom browser profile you can use almost any settings modification, imported certificate (to avoid another auth pop-up), use extensions, ...

    Basic auth pop-up you can avoid with send credentials in URL:

    driver.get("https://username:password@www.example.com");
    

    but it does not work in Chrome.