[英]How to override basic authentication in selenium2 with Java using chrome driver?
如何在selenium2 chrome驱动程序中覆盖基本身份验证? 我在我的项目中面临一个问题,即“需要身份验证”弹出窗口即将阻止webdriver继续导航。 请查找附带的截图。 我使用以下代码来实例化chrome驱动程序,
private WebDriver driver;
@Override
protected void setUp() throws Exception {
super.setUp();
System.setProperty("webdriver.chrome.driver", "C:/Selenium/chromedriver.exe");
driver = new ChromeDriver();
}
@Override
protected void tearDown() throws Exception {
// TODO Auto-generated method stub
super.tearDown();
}
能否请你帮忙 -
谢谢,
我在一个多小时内遇到了同样的问题,最后@ kenorb的解决方案救了我。 简而言之,您需要添加一个浏览器扩展,为您进行身份验证(因为Selenium本身不能这样做!)。
以下是Chrome和Python的工作原理:
background.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']
);
不要忘记将YOUR_PROXY_ *替换为您的设置。
的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"
}
将创建的proxy.zip添加为扩展名
from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_extension("proxy.zip") driver = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=chrome_options) driver.get("http://google.com") driver.close()
而已。 对我而言,就像一个魅力。 如果您需要动态创建proxy.zip或需要PHP示例,请转到原始帖子
我认为您正在谈论使用NTLM身份验证 ( Windows集成身份验证 )而不是基本身份验证 ( 您在URL中提供凭据 )。 假设您可以尝试在chrome中运行NTML auth:
转到Internet Explorer,然后打开“Internet选项”。 请遵循以下步骤:
完成这些更改后,您的chrome身份验证应该有效。 如果您想知道IE设置如何影响Chrome行为,那么Chrome会使用IE安全设置进行身份验证。
一些好的资源/信用:
将您的站点添加到本地Intranet ,不要更改任何用于用户身份验证的内容。 默认情况下,选择第二个选项( 仅在Intranet站点中自动登录 )。
您可以尝试将登录凭据添加到url get请求(在Java中):
driver.get("http://username:password@google.com/")
我设法两次发送凭据。 我不知道为什么,但是在第二次浏览器发送数据包时,身份验证标头会进行基本身份验证。
我的代码(使用C#):
string url = "http://user:password@google.com/";
IWebDriver webDriver = new ChromeDriver();
webDriver.Navigate().GoToUrl(url);
webDriver.Url = url;
除配置网络代理首选项外 ,您还可以在/etc/environment
设置http_proxy
。
其他方法是使用Chrome HTTP私有代理 (基于Chrome-proxy-helper )。
这是PHP示例(在README中找到):
$pluginForProxyLogin = '/tmp/a'.uniqid().'.zip';
$zip = new ZipArchive();
$res = $zip->open($pluginForProxyLogin, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->addFile('/path/to/Chrome-proxy-helper/manifest.json', 'manifest.json');
$background = file_get_contents('/path/to/Chrome-proxy-helper/background.js');
$background = str_replace(['%proxy_host', '%proxy_port', '%username', '%password'], ['5.39.64.181', '54991', 'd1g1m00d', '13de02d0e0z9'], $background);
$zip->addFromString('background.js', $background);
$zip->close();
putenv("webdriver.chrome.driver=/path/to/chromedriver");
$options = new ChromeOptions();
$options->addExtensions([$pluginForProxyLogin]);
$caps = DesiredCapabilities::chrome();
$caps->setCapability(ChromeOptions::CAPABILITY, $options);
$driver = ChromeDriver::start($caps);
$driver->get('https://old-linux.com/ip/');
header('Content-Type: image/png');
echo $driver->takeScreenshot();
unlink($pluginForProxyLogin);
同样的逻辑也适用于其他语言。
SeleniumHQ GitHub上已经报道了更便携的解决方案。
也可以看看:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.