【问题标题】:Chrome Headless + Proxy ServerChrome 无头 + 代理服务器
【发布时间】:2018-08-05 09:06:00
【问题描述】:

我在 Chrome 中使用 BrowserMob-Proxy,但是当我将其更改为无头模式时,请求/响应只是空的。这似乎是一个 SSL 问题,因为如果我尝试使用 http 网站,它就可以正常工作。知道可能是什么或我如何解决它吗?

我正在使用的代码:

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver
from browsermobproxy import Server
from pprint import pprint

MOBPATH = "/Users/tiagocardoso/Code/scraper/python/browsermob-proxy-2.1.4/bin/browsermob-proxy"

try:
    mobserver = Server(MOBPATH)
    mobserver.start()
    proxy = mobserver.create_proxy()

    chrome_options = webdriver.ChromeOptions()


    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    #chrome_options.add_argument('--no-sandbox')
    #chrome_options.add_argument('--window-size=800,600')
    #chrome_options.add_argument("--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36")

    chrome_options.add_argument('--ignore-certificate-errors')
    chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy))

    # Disable Images
    prefs = {
        "profile.managed_default_content_settings.images":2
    } 
    chrome_options.add_experimental_option("prefs",prefs)

    # Tried with those stuff...
    capabilities = DesiredCapabilities.CHROME.copy()
    capabilities['acceptSslCerts'] = True
    capabilities['acceptInsecureCerts'] = True

    driver = webdriver.Chrome(chrome_options=chrome_options,desired_capabilities=capabilities)

    proxy.new_har('google', options={"captureContent":True, "captureBinaryContent":True})
    driver.get('https://www.google.com')
    pprint(proxy.har) # returns [] if using headless

finally:
    driver.close()
    mobserver.stop()

【问题讨论】:

  • 我使用 Titanium 代理进行了相同的设置。 (我想收集饼干)。就我而言,我怀疑问题与代理没有生成有效的 HTTPS 证书有关。

标签: google-chrome selenium headless proxy-server


【解决方案1】:

我正在使用java,但我设法通过添加来解决它

 chromeOptions.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);

【讨论】:

    【解决方案2】:

    前段时间我遇到了类似的问题,最后我在无头环境中使用了virtual display

    这里解释了如何让它在 CLI 环境中启动浏览器:https://gist.github.com/addyosmani/5336747

    【讨论】:

    • 好吧,我刚刚尝试使用 Chrome Canary 并且效果很好,我只需要添加“--no-sandbox”参数。
    猜你喜欢
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多