我正在尝试使用Google Chrome浏览器进行selenium测试。我想使用HTTP基本身份验证登录。在Selenium中未实现,因此建议加载扩展。我正在使用来自的代码
https://github.com/RobinDev/Selenium-Chrome-HTTP-Private- Proxy,以及“如何使用chrome驱动程序使用Java覆盖selenium2中的基本身份验证”的答案? 我已尝试使其适应我的需求。
更新资料
查看最低工作示例。
git clone git@github.com:alexbiddle/selenium-chrome-http-basic-auth.git
摘录如下
var config = { mode: "fixed_servers", rules: { singleProxy: { scheme: "https", host: "subdomain.example.com" }, bypassList: ["foobar.com"] } }; chrome.proxy.settings.set({value: config, scope: "regular"}, function() {}); function callbackFn(details) { return { authCredentials: { username: "example", password: "abc123" } }; } chrome.webRequest.onAuthRequired.addListener( callbackFn, {urls: ["<all_urls>"]}, ['blocking'] );
使用Java将其加载
ChromeOptions chromeOptions = new ChromeOptions(); File proxyPath = new ClassPathResource("proxy.zip").getFile(); chromeOptions.addExtensions(proxyPath); DesiredCapabilities capability = DesiredCapabilities.chrome(); capability.setCapability(CAPABILITY, chromeOptions); webDriver = new ChromeDriver(capability);
我在https://developer.chrome.com/extensions/proxy#type- ProxyServer上仔细检查了文档,以防万一缺少某些值,但是在使用URL加载测试时
https://subdomain.example.com
它失败了
ERR_TUNNEL_CONNECTION_FAILED
我在Mac上使用Chrome。
该错误很可能是由于您的扩展程序定义的代理所致。
如果您需要与系统不同的扩展,则应构建不带代理的扩展,并在功能中定义代理。
要创建扩展名,只需使用username和中定义的凭据将以下文件压缩为password:
username
password
manifest.json :
{ "manifest_version": 2, "name": "Authentication for ...", "version": "1.0.0", "permissions": ["<all_urls>", "webRequest", "webRequestBlocking"], "background": { "scripts": ["background.js"] } }
background.js :
var username = "my-username"; var password = "my-password"; chrome.webRequest.onAuthRequired.addListener( function handler(details) { if (username == null) return {cancel: true}; var authCredentials = {username:username, password: username}; username = password = null; return {authCredentials: authCredentials}; }, {urls: ["<all_urls>"]}, ['blocking'] );