gpt4 book ai didi

python - 如何使用 selenium chrome 驱动程序 Python 3 登录代理服务器并进行身份验证?

转载 作者:太空宇宙 更新时间:2023-11-03 20:42:01 27 4
gpt4 key购买 nike

我正在尝试使用 python 3 和 selenium chrome 驱动程序登录到我的代理服务器,但是我无法通过右键单击身份验证窗口来找到我应该推送我的用户名的元素以及密码。

这是我尝试过的代码,但它卡在我必须插入用户名和密码的地方。

from selenium import webdriver
username = 'xxxxxxxxxxxxxxxxxxxxxx'
password = 'xxxxxxxxxxxxxxxxxxxxxx'
url = 'http://whatismyipaddress.com'
PROXY = "186.xxx.xx.xx:8000" # IP:PORT or HOST:PORT
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)
chrome = webdriver.Chrome(options=chrome_options)
chrome.get(url)

它打开浏览器并弹出一个小窗口,要求用户名和密码。如何直接用python插入它们?谢谢

最佳答案

像这样工作,请注意此代码的部分内容不是用 Python 编写的,只需复制并粘贴即可,同时确保 chromedriver.exe 位于同一目录中。

from selenium import webdriver
import os
import zipfile
url = 'https://whatismyipaddress.com/'
PROXY = 'XXX.XX.XX.XX' # YOUR PROXY IP ADDRESS
port = 'XXXX' # YOUR PORT NUMBER
user = 'XXXXX' # YOUR USER NAME
passw = 'XXXXX' # YOUR 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, port, user, passw)
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)
driver = webdriver.Chrome(
os.path.join(path, 'chromedriver'),
chrome_options=chrome_options)
return driver


driver = get_chromedriver(use_proxy=True)
driver.get(url)

只需复制并粘贴并更改您的凭据即可。确保不要更改任何缩进。干杯

关于python - 如何使用 selenium chrome 驱动程序 Python 3 登录代理服务器并进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56806592/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com