python+selenium+browsermobproxy 运行出错

配置好一切,但还是发生了报错,SSL协议错误,有解决方案告知一下。

selenium.common.exceptions.WebDriverException: Message: unknown error: net::ERR_SSL_PROTOCOL_ERROR (Session info: chrome=107.0.5304.107)

其他错误处理
修改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=

我的运行环境

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就是另一个不带后缀名的
server = Server(path=r"D:/browsermob-proxy-2.1.0/bin/browsermob-proxy.bat", options=options)
# 这里启动服务器,等会机会要关掉,不然下次用就端口占用冲突了
server.start()
# 设置trustAllServers为true,否则访问https网站报错
proxy = server.create_proxy({'trustAllServers': True})

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--proxy-server={}".format(proxy_server.proxy))  # Configure chrome options
# https需要加上,要不然回报安全连接问题
# 设置--ignore-certificate-errors,否则设置代理后的https网站无法正常访问
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')
# 此处最好暂停几秒等待页面加载完成,不然会拿不到结果
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']
     # 获取接口返回内容
     print(_response)

proxy_server.stop()
browser.quit()
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容