Chrome Headless + 代理服务器

3

我正在使用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()

我使用钛代理设置相同(我想收集cookies)。在我的情况下,我怀疑问题与代理未生成有效的HTTPS证书有关。 - realbart
2个回答

2
我正在使用Java,但是我通过添加代码成功解决了问题。
 chromeOptions.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);

1

好的,我刚刚尝试了Chrome Canary,效果很棒,我所需要做的就是添加“--no-sandbox”参数。 - tiagocardosoweb

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