且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-04 10:18:28

使用 switchTo().alert()当前实现 ChromeDriver Chrome 可能不是通过基本身份验证功能工作的***方式.取而代之的是,理想优雅的方法是将凭据嵌入子资源请求中.一个例子:

  • 代码块:

      import java.io.IOException;导入org.openqa.selenium.WebDriver;导入org.openqa.selenium.chrome.ChromeDriver;导入org.openqa.selenium.chrome.ChromeOptions;公共类BasicAuthentication_Chrome{公共静态无效的main(String [] args)引发InterruptedException,IOException{System.setProperty("webdriver.chrome.driver","C:\\ Utility \\ BrowserDrivers \\ chromedriver.exe");ChromeOptions选项=新的ChromeOptions();options.addArguments("start-maximized");WebDriver驱动程序=新的C​​hromeDriver(选项);driver.navigate().to("http://admin:admin@the-internet.herokuapp.com/basic_auth");}} 

  • 浏览器快照:

您可以在


Outro

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

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
      }

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