使用Browsermob代理,无法使用Selenium访问不安全的https网站

7
我正在尝试在我的Selenium(chrome)框架中嵌入Browsermob代理,以便拦截响应和其他网络请求,从而进行UI自动化测试。

描述:

Selenium webdriver使用Browsermob代理工作良好,HTTP和安全的HTTPS URL都可以正常工作。但当我尝试访问不安全的HTTPS URL时,会出现Chrome错误:ERR_TUNNEL_CONNECTION_FAILED。

以下是我的Python代码:

class Browser(object):
    display = None
    browser = None

    def __init__(self, implicitly_wait_seconds=10, is_visible=True, display_size=None, browser_name='chrome'):
        if not is_visible:
            self.display = Display(display_size)
        self.server = Server('/home/erez/Downloads/browsermob-proxy-2.1.4/bin/browsermob-proxy')
        self.server.start()
        self.proxy = self.server.create_proxy()
        self.capabilities = DesiredCapabilities.CHROME
        self.proxy.add_to_capabilities(self.capabilities)
        self.proxy.new_har("test", options={'captureHeaders': True, 'captureContent': True})
        self.start_browser(display_size, implicitly_wait_seconds, browser_name)

    def __enter__(self):
        return self

    def __exit__(self, _type, value, trace):
        self.close()

    def start_browser(self, display_size, implicitly_wait_seconds=10, browser_name='chrome'):
        if browser_name == 'chrome':
            chrome_options = Options()
            # chrome_options.add_argument("--disable-extensions")
            chrome_options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors"])
            chrome_options.add_argument("--ssl-version-max")
            chrome_options.add_argument("--start-maximized")
            chrome_options.add_argument('--proxy-server=%s' % self.proxy.proxy)
            chrome_options.add_argument('--ignore-certificate-errors')
            chrome_options.add_argument('--allow-insecure-localhost')
            chrome_options.add_argument('--ignore-urlfetcher-cert-requests')
            self.browser = webdriver.Chrome(os.path.dirname(os.path.realpath(__file__)) + "/chromedriver",
                                            chrome_options=chrome_options, desired_capabilities=self.capabilities)
            self.browser.implicitly_wait(implicitly_wait_seconds)
4个回答

6
您还可以使用trustAllServers参数调用create_proxy函数:
self.proxy = self.server.create_proxy(params={'trustAllServers':'true'})

3
如果安装Browsermobs/test服务器证书无法完成任务(如我的情况),虽然不是最优雅的方法,但可以解决问题:您可以通过向代理实例传递trustAllServers参数来绕过ERR_TUNNEL_CONNECTION_FAILED错误,这将禁用上游服务器的证书验证。不幸的是,据我所知,Browsermobs Python包装器尚未实现此功能。
然而,您可以通过Browsermobs REST API使用该参数启动代理(请参见REST API章节@https://github.com/lightbody/browsermob-proxy/blob/master/README.md)。在Python中,Requests可能是一个好选择。
以下是代码片段:
import json
import requests
from browsermobproxy import Server
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# Start the proxy via python
server = Server('/path_to/bin/browsermob-proxy')
server.start()

# Start proxy instance with trustAllServers set to true, store returned port number
r = requests.post('http://localhost:8080/proxy', data = {'trustAllServers':'true'})
port = json.loads(r.text)['port']

# Start Chromedriver, pass on the proxy
chrome_options = Options()
chrome_options.add_argument('--proxy-server=localhost:%s' % port)
driver = webdriver.Chrome('/path_to/selenium/chromedriver', chrome_options=chrome_options)

# Get a site with untrusted cert
driver.get('https://site_with_untrusted_cert')

此外,如果您需要访问HAR数据,您也需要通过REST API进行访问。
requests.put('http://localhost:8080/proxy/%s/har' % port, data = {'captureContent':'true'})

r = requests.get('http://localhost:8080/proxy/%s/har' % port)

当然,由于您正在禁用安全功能,因此此参数仅应用于有限的测试目的。

3
尝试使用这个。
self.capabilities['acceptSslCerts'] = True

3

我曾经遇到过使用BrowserMob代理进行SSL代理的相同问题。为此,您需要在浏览器中安装一个证书,该证书已在此链接中定义。

滚动到文档底部,您将看到一个名为“SSL支持”的部分。在浏览器中安装ca-certificate-rsa.cer,SSL代理就不会再出现问题了。


这对我没有起作用。下面opsec提供的使用REST的解决方案有效。 - Ganesh S

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接