配置好一切,但還是發(fā)生了報(bào)錯(cuò),SSL協(xié)議錯(cuò)誤,有解決方案告知一下。
selenium.common.exceptions.WebDriverException: Message: unknown error: net::ERR_SSL_PROTOCOL_ERROR (Session info: chrome=107.0.5304.107)
其他錯(cuò)誤處理
修改browsermob-proxy.bat文件,增加行set JAVA_OPTS
set JAVA_OPTS=--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/sun.net.util=ALL-UNNAMED
set EXTRA_JVM_ARGUMENTS=
我的運(yùn)行環(huán)境
java jdk:18.0.1
python: 3.8
selenium: 4.1.3
browsermobproxy : 0.5.0
browsermob-proxy: 2.1.0
chrome: 107.0.5304.107
system:windows 10
我的代碼如下
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver
from browsermobproxy import Server
import time
options = {'port': 8080}
# Windows就是bat,如果Linux就是另一個(gè)不帶后綴名的
server = Server(path=r"D:/browsermob-proxy-2.1.0/bin/browsermob-proxy.bat", options=options)
# 這里啟動(dòng)服務(wù)器,等會(huì)機(jī)會(huì)要關(guān)掉,不然下次用就端口占用沖突了
server.start()
# 設(shè)置trustAllServers為true,否則訪問https網(wǎng)站報(bào)錯(cuò)
proxy = server.create_proxy({'trustAllServers': True})
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--proxy-server={}".format(proxy_server.proxy)) # Configure chrome options
# https需要加上,要不然回報(bào)安全連接問題
# 設(shè)置--ignore-certificate-errors,否則設(shè)置代理后的https網(wǎng)站無法正常訪問
chrome_options.add_argument("--ignore-certificate-errors")
desired_capabilities = DesiredCapabilities()
capabilities = desired_capabilities.CHROME.copy()
proxy_server.new_har("test", options={'captureHeaders': True, 'captureBinaryContent': True, 'captureContent': True})
driver = webdriver.Chrome(options=chrome_options, desired_capabilities=capabilities)
driver.get('http://baidu.com')
# 此處最好暫停幾秒等待頁面加載完成,不然會(huì)拿不到結(jié)果
time.sleep(3)
result = proxy_server.har
for entry in result['log']['entries']:
_url = entry['request']['url']
print(_url)
_response = entry['response']
_content = _response['content']
# 獲取接口返回內(nèi)容
print(_response)
proxy_server.stop()
browser.quit()