selenium - 使用 Chrome 的 Selenium 代理身份验证不允许 https 请求
问题描述
我正在尝试构建一个运行 selenium webdriver 的 python 脚本,进行代理身份验证(使用用户和密码)并通过 https 调用加载网页。我搜索了互联网,找到了成功设置 http 代理连接的最新方法。但是,当尝试通过 https 加载网页时,而不是像我看到的示例那样使用 http(这是我需要的),Chrome 驱动程序会显示“页面永久移动或无法访问”。
下面是我使用的代码。
- 更多细节:
- 代理只能通过 80 端口作为 http 访问。
- 尝试使用 http 加载同一页面似乎确实“通过”,但自然地,该页面并未完全加载。
- 下面的代码并不是什么新鲜事。有几个参考资料准确地表明了这一点。唯一的区别是我需要通过 https 访问所需的网页
- 我实际上不负责代理设置,所以如果改变事情是让它工作的唯一方法,那将是一个挑战。
任何帮助都将不胜感激。肿瘤坏死因子。
硒脚本代码:
PROXY_HOST = 'http://www.myproxy.com'
PROXY_PORT = 80
PROXY_USER = 'user'
PROXY_PASS = '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"
}
"""
background_js = """
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: "%s",
port: parseInt(%s)
},
bypassList: ["localhost"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "%s",
password: "%s"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
""" % (PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS)
def get_chromedriver(use_proxy=False, user_agent=None):
path = os.path.dirname(os.path.abspath(__file__))
chrome_options = webdriver.ChromeOptions()
if use_proxy:
pluginfile = 'proxy_auth_plugin.zip'
with zipfile.ZipFile(pluginfile, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
chrome_options.add_extension(pluginfile)
if user_agent:
chrome_options.add_argument('--user-agent=%s' % user_agent)
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--start-maximized')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('window-size=1200,1100')
driver = webdriver.Chrome(
os.path.join(path, 'chromedriver'),
chrome_options=chrome_options)
return driver
driver = get_chromedriver(use_proxy=True)
driver.get("https://www.msysecurepage.com")
解决方案
推荐阅读
- sql - Visual Basic 6 和 dbf:'join' 和 'where' 的问题
- c - 如何将位于结构数组内的一个指针内容复制到另一个指针
- c# - Mongodb 更新或插入 c#
- android - 任务应用程序执行失败:generateDebugRFile 错误:“:”不是有效的资源名称字符
- .net - 如何让 DtsClient 使用正确版本的集成服务
- asp.net - 使用(Azure 活动目录 b2c)Json Web 令牌验证的 ASP.NET SignalR 客户端-服务器身份验证
- python - 需要帮助继续获取对象“模块”没有属性“滚动平均”
- routing - BIG-IP F5 上基于源的路由(基于策略的路由)
- ignite - 点燃事务失败无法通过持久性恢复
- laravel-5 - 学说/dbal 未安装在实时服务器上