分享页面
拖动logo到书签栏,立即收藏青果网络
控制台
|
登录/注册
子
用户在实名认证前无法开通业务
充值
未支付订单
未完成工单
未读消息
会员中心
退出登录
文档中心
产品介绍
使用指南
开发者指南
API接口
代码示例
常见问题
账户中心
查询
开发者指南
开发者指南
短效代理开发者指南
隧道代理开发者指南
长效代理开发者指南
独享代理开发者指南
短效代理(全球HTTP)开发者指南
隧道代理(全球HTTP)开发者指南
通用功能
测试代理的可用性
国内区域编码
海外区域编码
如何使用API鉴权
开发者指南
短效代理开发者指南
隧道代理开发者指南
长效代理开发者指南
独享代理开发者指南
短效代理(全球HTTP)开发者指南
隧道代理(全球HTTP)开发者指南
通用功能
测试代理的可用性
国内区域编码
海外区域编码
如何使用API鉴权
开发者指南
>
开发者指南
>
Python自动化测试Selenium+chrome连接代理ip(账密模式)
Python自动化测试Selenium+chrome连接代理ip(账密模式)
发布时间:2022-09-16 14:16
此示例Python使用Selenium调用Chrome浏览器并通过代理进行自动化测试。 > 请注意:目前最高支持到Chrome 129.0.6614.3,后续版本暂不支持 - 代码示例 ```python import time import string import zipfile from selenium import webdriver from selenium.webdriver.chrome.service import Service targetURL = "http://myip.ipip.net" # 访问的目标站点 proxyHost = "61.171.76.145" # 代理IP地址 proxyPort = "50353" # 代理IP端口号 authKey = "x" # 代理IP的AuthKey password = "x" # 代理IP的AuthPwd def create_proxy_auth_extension(proxy_host, proxy_port, proxy_username, proxy_password, scheme='http', plugin_path=None): if plugin_path is None: plugin_path = r'./{}_{}_qgnet_proxyauth_plugin.zip'.format(proxy_username, proxy_password) manifest_json = """ { "version": "1.0.0", "manifest_version": 2, "name": "Chrome Proxy", "permissions": [ "proxy", "tabs", "unlimitedStorage", "storage", "
", "webRequest", "webRequestBlocking" ], "background": { "scripts": ["background.js"] }, "minimum_chrome_version":"22.0.0" } """ background_js = string.Template( """ var config = { mode: "fixed_servers", rules: { singleProxy: { scheme: "${scheme}", host: "${host}", port: parseInt(${port}) }, bypassList: ["localhost"] } }; chrome.proxy.settings.set({value: config, scope: "regular"}, function() {}); function callbackFn(details) { return { authCredentials: { username: "${username}", password: "${password}" } }; } chrome.webRequest.onAuthRequired.addListener( callbackFn, {urls: ["
"]}, ['blocking'] ); """ ).substitute( host=proxy_host, port=proxy_port, username=proxy_username, password=proxy_password, scheme=scheme, ) with zipfile.ZipFile(plugin_path, 'w') as zp: zp.writestr("manifest.json", manifest_json) zp.writestr("background.js", background_js) return plugin_path if __name__ == '__main__': # 此处指定您的webdriver路径,版本需要跟您所使用的Chrome版本一致, # 下载地址https://registry.npmmirror.com/binary.html?path=chromedriver/ driver_location = "./chromedriver/chromedriver_v106_win.exe" proxy_auth_plugin_path = create_proxy_auth_extension( proxy_host=proxyHost, proxy_port=proxyPort, proxy_username=authKey, proxy_password=password) option = webdriver.ChromeOptions() option.add_argument("--start-maximized") # 窗口最大化运行 option.add_extension(proxy_auth_plugin_path) # 添加proxy插件 # 此处selenium版本为4.8.0 driver = webdriver.Chrome(service=Service(driver_location), options=option) driver.get(targetURL) time.sleep(100) driver.quit() ``` - 运行结果 [](https://cos.qg.net/uploads/image/20220916/d1a8b394a7d64cdd99b120803390ac56.png)
上一篇:没有了
下一篇:没有了
相关文章
短效代理(全球HTTP)开发者指南
隧道代理(全球HTTP)-产品介绍
xshell如何设置代理IP
隧道代理(全球HTTP)开发者指南
短效代理-弹性提取
傲游浏览器如何使用代理IP
本文导读