[英]How to handle authentication alert of browser with selenium webdriver using java without autoit and url method
I tried to set proxy with我试图设置代理
Proxy proxy = new Proxy();
proxy.setHttpProxy("http://MY_USERNAME:MY_PASSWORD@MY_HOST:MY_PORT");
It is redirecting to specific URL but it is not actually setting proxy and giving me local IP instead MY_HOST.它正在重定向到特定的 URL,但实际上并没有设置代理,而是给我本地 IP,而不是 MY_HOST。
I can not use autoit script.我不能使用 autoit 脚本。
Please guide me that how can I handle alert box请指导我如何处理警报框
enter image description here在此处输入图片说明
I have tried with driver.switchTo().alert();
我试过driver.switchTo().alert();
but, the code is not working after the statement driver.get(MY_URL);
但是,在语句driver.get(MY_URL);
之后代码driver.get(MY_URL);
when the popup appears.当弹出窗口出现时。
Note : Both options (1) by set crx file and (2) by giving user name and password in URL with host and port, are authenticating successfully but please note that it is not actually set proxy as required but instead it gives local IP注意:这两个选项(1)通过设置 crx 文件和(2)通过在带有主机和端口的 URL 中提供用户名和密码,都已成功进行身份验证,但请注意,它实际上并未按要求设置代理,而是提供了本地 IP
java.awt.Robot class can be used for authentication java.awt.Robot类可用于身份验证
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
login() throws Exception {
// Pass username
autoType(username);
// to move to Password field
autoTab();
// Enter Password
autoType(password);
// To click on login
autoSubmit();
}
private static void autoType(String string) throws AWTException {
Robot robot = new Robot();
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection stringSelection = new StringSelection(string);
clipboard.setContents(stringSelection, null);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
}
private static void autoTab() throws AWTException {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
}
private static void autoSubmit() throws AWTException {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
You can create a Chrome extension that can handle the proxy on the fly.您可以创建一个可以即时处理代理的 Chrome 扩展程序。 ChromeDriver does not provide any capability to handle HTTP proxy that needs credentials. ChromeDriver 不提供任何处理需要凭据的 HTTP 代理的功能。
Create a zip file proxyExtension.zip
that contains the following 2 files;创建一个包含以下 2 个文件的 zip 文件proxyExtension.zip
;
background.js背景.js
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: "YOU_PROXY_ADDRESS",
port: parseInt(YOUR_PROXY_PORT)
},
bypassList: ["foobar.com"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "YOUR_PROXY_USERNAME",
password: "YOUR_PROXY_PASSWORD"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
YOU_PROXY_ADDRESS
, YOUR_PROXY_PORT
, YOUR_PROXY_USERNAME
, YOUR_PROXY_PASSWORD
fields will be replaced with your informations. YOU_PROXY_ADDRESS
、 YOUR_PROXY_PORT
、 YOUR_PROXY_USERNAME
、 YOUR_PROXY_PASSWORD
字段将替换为您的信息。
manifest.json清单文件
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}
Then, initialize the webdriver with the following code;然后,使用以下代码初始化 webdriver;
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addExtensions(new File("path_to_extension_file/proxyExtension.zip"));
WebDriver driver = new ChromeDriver(chromeOptions);
Please change the path_to_extension_file
to your directory that has the proxyExtension.zip
file.请将path_to_extension_file
更改为包含proxyExtension.zip
文件的目录。
You can also find more information on the link .您还可以在 链接上找到更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.