Chrome无头+代理服务器

时间:2018-02-25 23:18:13

标签: google-chrome selenium headless proxy-server

我正在使用BrowserMob-Proxy和Chrome,但当我将其更改为无头模式时,请求/响应只是空的。这似乎是一个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()

1 个答案:

答案 0 :(得分:1)

我正在使用Java,但是我设法通过添加

解决了该问题
 chromeOptions.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);