无法处理无头Chrome中的Microsoft登录身份验证弹出窗口[使用Java的硒]

sat*_*iya 3 selenium google-chrome windows-authentication selenium-chromedriver google-chrome-headless

我正在自动化Web应用程序以在Headless Chrome中运行。ChromeDriver版本:-ChromeDriver 74.0.3729.6应用程序登录屏幕弹出窗口,用于输入用户名和密码。我使用警报来处理普通Chrome中的弹出窗口

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

将Chrome设置为无头时,则不会显示Windows弹出窗口。我只能在屏幕截图中看到黑屏。

另外,我尝试将chromeoptions添加为

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);
Run Code Online (Sandbox Code Playgroud)

ChromeDriverLog的默认值为

"default_content_settings": {
         "geolocation": 1,
         "mouselock": 1,
         "notifications": 1,
         "popups": 1,
         "ppapi-broker": 1
      }
Run Code Online (Sandbox Code Playgroud)

Deb*_*anB 5

在当前的ChromeDriverChrome实施中,使用switchTo().alert()并非通过基本身份验证功能的最佳方式。取而代之,一种理想优雅的方法是将凭据嵌入子资源请求中。一个例子:

  • 代码块:

    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");
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 浏览器快照:

基本认证

您可以在Selenium-通过URL的基本身份验证中找到相关的详细讨论。


无头模式下的基本身份验证

随着--headless模式的存在启用 基本身份验证仍然按预期工作。一个例子:

  • 代码块:

    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"));
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 浏览器快照:

基本认证


奥托罗

Python Windows身份验证用户名和密码不起作用