我正在自动化 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()
实现的 ChromeDriver 和 Chrome 可能不是解决问题的最佳方法< 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"); } }
浏览器快照:
<小时/>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")); } }
浏览器快照:
片尾
Python Windows Authentication username and password is not working
关于selenium - 无法处理 headless Chrome 中的 Microsoft 登录身份验证弹出窗口[使用 java 的 Selenium],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56341954/