具有公司代理身份验证的 Chrome 无头浏览器无法正常工作

人气:187 发布:2022-10-16 标签: basic-authentication proxy selenium-chromedriver headless chrome-options

问题描述

我正在尝试运行位于公司代理后面的 Chrome 无头浏览器.我试过下面的代码.但无法通过.

I am trying to run a Chrome headless browser sitting behind a corporate proxy. I tried below code. But unable to pass through it.

public class HeadlessChrome 
{
    WebDriver driver;

    @Test
    public void createChromeDriverHeadless() throws InterruptedException
    {
        System.setProperty("webdriver.chrome.driver", "D:\LocalData\workspace\Drivers and Libraries\driver\chromedriver.exe");

        ChromeOptions chromeOptions = new ChromeOptions();

        Proxy proxy = new Proxy();
        proxy.setHttpProxy("http://user:pwd@server:port");
        proxy.setSslProxy("http://user:pwd@server:port");

//      chromeOptions.setCapability("proxy", proxy);
        chromeOptions.addArguments("--proxy-server=user:pwd@server:port");
        chromeOptions.addArguments("--headless");
        chromeOptions.addArguments("--disable-gpu");
        chromeOptions.addArguments("start-maximized");  

        driver = new ChromeDriver(chromeOptions);
        driver.get("http://seleniumhq.org");       

        Thread.sleep(5000);
        System.out.println("Title : " + driver.getTitle());
        assertTrue(driver.findElement(By.id("q")).isDisplayed());
        driver.quit();
    }
}

请帮帮我.

推荐答案

如果你没有使用 headless 你可以使用下面链接中的方法

If you were not using headless you could have used the approach in below link

用户:使用硒传递代理

但目前不允许使用无头扩展.所以现在你的选择是添加另一个代理

But with headless extension are currently not allowed. So now your option is add another proxy

chrome -> (intermediate proxy w/o auth) -> corporate proxy w/ auth -> internet

一种选择是使用 polipo

https://www.irif.fr/~jch/software/polipo/

配置如下

parentAuthCredentials=username:password
parentProxy=corporateproxy:port

然后使用

chromeOptions.addArguments("--proxy-server=http://polipoproxy:port");

默认值为 127.0.0.1:8123 in don't override in polipo config.

The default would be 127.0.0.1:8123 in don't override in polipo config.

您可以使用的其他选项

使用 squid 代理而不是 polipo

Use squid proxy instead of polipo

使用 python 或 node 或任何其他您熟悉的语言编写您自己的代理转发器

Write your own proxy forwarder using python or node or any other language you are comfortable with

201