selenium - 无法处理 headless Chrome 中的 Microsoft 登录身份验证弹出窗口[使用 java 的 Selenium]

标签 selenium google-chrome selenium-chromedriver windows-authentication 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(); 

当 Chrome 设置为 headless 时,不会显示窗口弹出窗口。我只能在屏幕截图中看到空白屏幕。

此外,我尝试将 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);

ChromeDriverLog 的默认值为

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

最佳答案

当前使用 switchTo().alert() 实现的 ChromeDriverChrome 可能不是解决问题的最佳方法< strong>基本身份验证功能。相反,一种理想优雅的方式是将凭据嵌入到子资源请求中。一个例子:

  • 代码块:

    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");
        }
    }
    
  • 浏览器快照:

Basic Auth

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

<小时/>

headless 模式下的基本身份验证

--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"));
        }
    }
    
  • 浏览器快照:

Basic Auth

<小时/>

片尾

Python Windows Authentication username and password is not working

关于selenium - 无法处理 headless Chrome 中的 Microsoft 登录身份验证弹出窗口[使用 java 的 Selenium],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56341954/

相关文章:

java - 从 Linux 终端启动 selenium webdriver 测试

java - Cucumber Scenario Outline 上的 Example 表是否可以有空值?

css - 减少屏幕宽度后网站页脚上的链接将不起作用

google-chrome - Chrome CSS 背景颜色问题

ruby - Chromedriver `driver.manage.logs.get(:browser)` 在 chromedriver 75.0.3770.8 上失败

javascript - 将元素拖放到特定位置 - Selenium、WebDriverJS

java - Selenium 偶尔出现 UnreachableBrowserException

javascript - 谷歌浏览器应用程序开发是否允许您将 html 和 javascript 添加到本地存储的文件中,具体取决于用户

python - 如何使用通过 webdriver_manager 安装的 ChromeDriver 更改 Google Chrome UserAgent

python - 下载 PDF 作为文件对象,无需使用 Python 中的 Chrome 和 Selenium 下载文件