Unable to handle Microsoft login authentication popup in headless chrome[Selenium using java]

被刻印的时光 ゝ 提交于 2020-01-04 05:20:14

问题


I am automating the web application to run in Headless Chrome. ChromeDriver Version:- ChromeDriver 74.0.3729.6 Application Login screen has windows popup to enter username and password. I used alerts to handle the popup in normal chrome

WebDriverWait wait = new WebDriverWait(driver, 18);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.sendKeys("username" + Keys.TAB + "password");
alert.accept(); 

When Chrome is set to headless, windows popup is not displaying. I could only see blank screen in screenshots.

Also, i tried adding the chromeoptions as

String path = "path to chromedriver";
System.setProperty("webdriver.chrome.driver", path);
System.setProperty("webdriver.chrome.logfile", "./chromedriver.log");
System.setProperty("webdriver.chrome.verboseLogging", "true");
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("--disable-popup-blocking");
driver = new ChromeDriver(options);

ChromeDriverLog has the default values as

"default_content_settings": {
         "geolocation": 1,
         "mouselock": 1,
         "notifications": 1,
         "popups": 1,
         "ppapi-broker": 1
      }

回答1:


With the current implementation of ChromeDriver and Chrome using switchTo().alert() may not the best possible way to work through Basic Authentication functionality. Instead, an ideal and elegant way would be to embedded the credentials in subresource requests. An example:

  • Code Block:

    import java.io.IOException;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    
    public class BasicAuthentication_Chrome 
    {
        public static void main(String[] args) throws InterruptedException, IOException 
        {
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            WebDriver driver =  new ChromeDriver(options);
            driver.navigate().to("http://admin:admin@the-internet.herokuapp.com/basic_auth");
        }
    }
    
  • Browser Snapshot:

You can find a relevant detailed discussion in Selenium - Basic Authentication via url


Basic Authentication in headless mode

With --headless mode being enabled Basic Authentication still works as expected. An example:

  • Code Block:

    import java.io.File;
    import java.io.IOException;
    
    import org.apache.commons.io.FileUtils;
    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.TakesScreenshot;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    
    public class BasicAuthentication_Chrome 
    {
        public static void main(String[] args) throws InterruptedException, IOException 
        {
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            options.addArguments("--headless");
            WebDriver driver =  new ChromeDriver(options);
            driver.navigate().to("http://admin:admin@the-internet.herokuapp.com/basic_auth");
            Thread.sleep(3000);
            File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(scrFile, new File(".\\Screenshots\\headless.png"));
        }
    }
    
  • Browser Snapshot:


Outro

Python Windows Authentication username and password is not working



来源:https://stackoverflow.com/questions/56341954/unable-to-handle-microsoft-login-authentication-popup-in-headless-chromeseleniu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!