我遵循了@obiwankoban和@SubjectiveReality在以下链接中提到的解决方案: How to handle authentication popup in Chrome with Selenium WebDriver using Java
我执行了以下步骤:
在扩展文件夹内的以下两个文件中创建。 manifest.json
{ “ name”:“ Webrequest API”, “ version”:“ 1.0”, “ description”:“扩展以处理身份验证窗口”, “权限”:[ “ webRequest”, “ webRequestBlocking”, ” ], “背景”: { “脚本”:[ “ webrequest.js” ] }, “ manifest_version”:2 }
webrequest.js
chrome.webRequest.onAuthRequired.addListener(function(details){
console.log("chrome.webRequest.onAuthRequired event has fired");
return {
authCredentials: {username: "getestone", password: "Viacom@2020"}
};
},
{urls:["<all_urls>"]},
['blocking']);
硒代码
public class openWebsite {
public static void main(String[] args) {
Properties prop = new Properties();
ChromeOptions options = new ChromeOptions();
File addonpath = new
File("C:\\Users\\10642575\\Desktop\\Work\\Automation\\AuthPopUp\\Extension.crx");
ChromeOptions chrome = new ChromeOptions();
chrome.addExtensions(addonpath);
//options.addArguments("--no-sandbox");
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\10642575\\Desktop\\Work\\Automation\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
driver.get("https://uat.contentplatform.viacom.com/");
driver.findElement(By.xpath("//input[@type='email']")).sendKeys("getest.one@viacom.com");
driver.findElement(By.xpath("//input[@type='submit']")).click();
}
}
按照上述步骤操作后,在chrome上启动网站时,仍然会弹出服务器身份验证。身份验证绕过不起作用。
PLZ帮助