Selenium ChromeDriverManager Web驱动程序安装未通过代理进行更新



我需要使用selenium访问网站,使用python访问Chrome。下面是我的代码的缩短版本。

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
# PROXY='https://myproxy.com:3128'
PROXY = 'https://username:password@myproxy.com:3128'
proxyuser='username' #this is the proxy user name used in above string
proxypwd='password' #this is the proxy password used in above string

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)
chrome_options.add_argument("ignore-certificate-errors");
chrome = webdriver.Chrome(options=chrome_options,service=Service(ChromeDriverManager().install()))
chrome.get("https://www.google.com")
while True:
print('ok')

我在一个需要身份验证的公司代理服务器后面。我不知道如何通过chromedriver安装的登录凭据和代理设置当上面的代码在没有代理的情况下运行时,它可以按预期工作。但当使用代理连接运行时,它会给出如下错误:

[WDM] - ====== WebDriver manager ======
[WDM] - Current google-chrome version is 105.0.5195
[WDM] - Get LATEST chromedriver version for 105.0.5195 google-chrome
Traceback (most recent call last):
File "D:PythonPython39libsite-packagesurllib3connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "D:PythonPython39libsite-packagesurllib3connectionpool.py", line 386, in _make_request
self._validate_conn(conn)
File "D:PythonPython39libsite-packagesurllib3connectionpool.py", line 1040, in _validate_conn
conn.connect()
File "D:PythonPython39libsite-packagesurllib3connection.py", line 414, in connect
self.sock = ssl_wrap_socket(
File "D:PythonPython39libsite-packagesurllib3utilssl_.py", line 449, in ssl_wrap_socket
ssl_sock = _ssl_wrap_socket_impl(
File "D:PythonPython39libsite-packagesurllib3utilssl_.py", line 493, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
File "D:PythonPython39libssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "D:PythonPython39libssl.py", line 1040, in _create
self.do_handshake()
File "D:PythonPython39libssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:PythonPython39libsite-packagesrequestsadapters.py", line 489, in send
resp = conn.urlopen(
File "D:PythonPython39libsite-packagesurllib3connectionpool.py", line 785, in urlopen
retries = retries.increment(
File "D:PythonPython39libsite-packagesurllib3utilretry.py", line 550, in increment
raise six.reraise(type(error), error, _stacktrace)
File "D:PythonPython39libsite-packagesurllib3packagessix.py", line 769, in reraise
raise value.with_traceback(tb)
File "D:PythonPython39libsite-packagesurllib3connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "D:PythonPython39libsite-packagesurllib3connectionpool.py", line 386, in _make_request
self._validate_conn(conn)
File "D:PythonPython39libsite-packagesurllib3connectionpool.py", line 1040, in _validate_conn
conn.connect()
File "D:PythonPython39libsite-packagesurllib3connection.py", line 414, in connect
self.sock = ssl_wrap_socket(
File "D:PythonPython39libsite-packagesurllib3utilssl_.py", line 449, in ssl_wrap_socket
ssl_sock = _ssl_wrap_socket_impl(
File "D:PythonPython39libsite-packagesurllib3utilssl_.py", line 493, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
File "D:PythonPython39libssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "D:PythonPython39libssl.py", line 1040, in _create
self.do_handshake()
File "D:PythonPython39libssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:eclipse-workspaceEssential Trainingtest.py", line 17, in <module>
chrome = webdriver.Chrome(options=chrome_options,service=Service(ChromeDriverManager().install()))
File "D:PythonPython39libsite-packageswebdriver_managerchrome.py", line 37, in install
driver_path = self._get_driver_path(self.driver)
File "D:PythonPython39libsite-packageswebdriver_managercoremanager.py", line 29, in _get_driver_path
binary_path = self.driver_cache.find_driver(driver)
File "D:PythonPython39libsite-packageswebdriver_managercoredriver_cache.py", line 95, in find_driver
driver_version = driver.get_version()
File "D:PythonPython39libsite-packageswebdriver_managercoredriver.py", line 42, in get_version
self.get_latest_release_version()
File "D:PythonPython39libsite-packageswebdriver_managerdriverschrome.py", line 44, in get_latest_release_version
resp = self._http_client.get(url=latest_release_url)
File "D:PythonPython39libsite-packageswebdriver_managercorehttp.py", line 31, in get
resp = requests.get(url=url, verify=self._ssl_verify, **kwargs)
File "D:PythonPython39libsite-packagesrequestsapi.py", line 73, in get
return request("get", url, params=params, **kwargs)
File "D:PythonPython39libsite-packagesrequestsapi.py", line 59, in request
return session.request(method=method, url=url, **kwargs)
File "D:PythonPython39libsite-packagesrequestssessions.py", line 587, in request
resp = self.send(prep, **send_kwargs)
File "D:PythonPython39libsite-packagesrequestssessions.py", line 701, in send
r = adapter.send(request, **kwargs)
File "D:PythonPython39libsite-packagesrequestsadapters.py", line 547, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

由于公司政策,不可能绕过代理

request("get", url, params=params, **kwargs)

我认为这一行的错误就是问题所在。驱动程序似乎无法确定网络上可用的新版本,因为这里的请求函数应该有代理设置-我看不到**kwargs值,理想情况下-我认为**kwarg应该有代理参数。-我不确定,但是。

我对代码进行了如下修改。基本上,我现在已经使用了自定义下载管理器这是令人担忧的

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.download_manager import WDMDownloadManager
from webdriver_manager.core.http import HttpClient
from selenium.webdriver.chrome.service import Service
from requests import Response
import urllib3
import requests
import os
os.environ['WDM_SSL_VERIFY'] = '0'
capabilities = webdriver.DesiredCapabilities.CHROME
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
PROXY = "https://username:password@myproxy.com:3128"
proxyuser='username'
proxypwd='password'
opt = webdriver.ChromeOptions()
opt.add_argument('--proxy-server=%s' % PROXY)
opt.add_argument("ignore-certificate-errors")
class CustomHttpClient(HttpClient):
def get(self, url, params=None) -> Response:
proxies={'http': 'http://username:password@myproxy.com:3128',
'https': 'http://username:password@myproxy.com:3128',
}
return requests.get(url, params,proxies=proxies,verify=False)
http_client = CustomHttpClient()
download_manager = WDMDownloadManager(http_client)
chrome = webdriver.Chrome(service=Service(ChromeDriverManager(download_manager=download_manager).install()),options=opt)
chrome.get("https://www.google.com")
while True:
print('ok')

最新更新