无法处理无头Chrome中的Microsoft登录身份验证弹出窗口[使用Java的硒] [英] Unable to handle Microsoft login authentication popup in headless chrome[Selenium using java]
问题描述
我正在自动化Web应用程序以使其在Headless Chrome中运行.ChromeDriver版本:-ChromeDriver 74.0.3729.6应用程序登录"屏幕上会弹出窗口,用于输入用户名和密码.我使用警报来处理普通Chrome浏览器中的弹出窗口
WebDriverWait等待=新的WebDriverWait(驱动程序,18);wait.until(ExpectedConditions.alertIsPresent());警报警报= driver.switchTo().alert();alert.sendKeys(用户名" + Keys.TAB +密码");alert.accept();
将Chrome设置为无头时,则不会显示Windows弹出窗口.我只能在屏幕截图中看到空白屏幕.
此外,我尝试将chromeoptions添加为
字符串路径="chromedriver的路径";System.setProperty("webdriver.chrome.driver",path);System.setProperty("webdriver.chrome.logfile","./chromedriver.log");System.setProperty("webdriver.chrome.verboseLogging","true");ChromeOptions选项=新的ChromeOptions();options.addArguments(-headless");options.addArguments(-disable-gpu");options.addArguments(-disable-popup-blocking");driver = new ChromeDriver(options);
ChromeDriverLog的默认值为
"default_content_settings":{地理位置":1,"mouselock":1,通知":1,弹出式窗口":1,"ppapi-broker":1}
使用 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驱动程序=新的ChromeDriver(选项);driver.navigate().to("http://admin:admin@the-internet.herokuapp.com/basic_auth");}}
-
浏览器快照:
您可以在
Outro
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
这篇关于无法处理无头Chrome中的Microsoft登录身份验证弹出窗口[使用Java的硒]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!