如何使用Java代码在Selenium中为Chrome浏览器设置代理

13

我希望运行我的Selenium Java代码以测试一个网页。但由于网络限制,该网页没有加载。当我手动设置代理并在浏览器中输入网址时可以正常工作。现在我需要在运行Selenium代码时传递这些代理设置。请帮帮我。

我尝试了下面的代码,但仍然显示相同的错误:

Proxy p=new Proxy();


// Set HTTP Port to 7777
p.setHttpProxy("www.abc.com:8080");

// Create desired Capability object
DesiredCapabilities cap=new DesiredCapabilities();

// Pass proxy object p
cap.setCapability(CapabilityType.PROXY, p);

// Open  firefox browser
WebDriver driver=new ChromeDriver(cap);

将Capabilities对象传递给ChromeDriver()构造函数已被弃用。 - Stéphane GRILLON
7个回答

11

向ChromeDriver()构造函数传递Capabilities对象已过时。使用代理的一种方法是:

String proxy = "127.0.0.1:5000";
ChromeOptions options = new ChromeOptions().addArguments("--proxy-server=http://" + proxy);
WebDriver webDriver = new ChromeDriver(options);

8
将Capabilities对象传递给ChromeDriver()构造函数已被弃用。您可以在此处找到新的官方文档。
ChromeOptions chromeOptions = new ChromeOptions();

Proxy proxy = new Proxy();
proxy.setAutodetect(false);
proxy.setHttpProxy("http_proxy-url:port"); 
proxy.setSslProxy("https_proxy-url:port");
proxy.setNoProxy("no_proxy-var");

chromeOptions.setCapability("proxy", proxy); 
driver = new ChromeDriver(chromeOptions);

尝试在Selenium 4中使用恶意代理,但仍然连接到网站 :(。 - Dean Hiller

7
问题已通过以下代码解决 -
Proxy proxy = new Proxy(); 
proxy.setHttpProxy("yoururl:portno"); 
proxy.setSslProxy("yoururl:portno"); 

DesiredCapabilities capabilities = DesiredCapabilities.chrome(); 
capabilities.setCapability("proxy", proxy); 

ChromeOptions options = new ChromeOptions(); 
options.addArguments("start-maximized"); 

capabilities.setCapability(ChromeOptions.CAPABILITY, options); 

driver = new ChromeDriver(capabilities);

嗨@Praveen Medipally,使用上面的代码在浏览器中出现"This site can’t be reached"错误。我有代理服务器的用户名和密码,我将其注入到我的URL中,如http://user:pwd@proxy:port 。你还能用以上的代码吗? - Solomon Raja
4
向 ChromeDriver() 构造函数传递一个 Capabilities 对象已经不再推荐使用。 - Stéphane GRILLON

4
DesiredCapabilities dc;
dc = DesiredCapabilities.chrome();              
System.setProperty("http.proxyHost", "127.0.0.1");
System.setProperty("http.proxyPort", "9090");
System.setProperty("https.proxyHost", "127.0.0.1");
System.setProperty("https.proxyPort", "9090");                      
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("--disable-extensions");
dc.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(dc);

嗨,巴尼,感谢您的回复。但是当我尝试实现上述代码时,仍然显示相同的错误 -“无法访问此网站”。 - Praveen Medipally
1
将一个Capabilities对象传递给ChromeDriver()构造函数已经被弃用。 - Stéphane GRILLON
在Selenium 4 @Barney中尝试使用了错误的IP地址,但出于某种原因仍然能够访问网站:(。 - Dean Hiller

3

另一种做法:

        boolean useProxy = true;
        ChromeOptions options = new ChromeOptions().addArguments(
                '--headless',
                '--no-sandbox',
                '--disable-extensions',
                '--proxy-bypass-list=localhost');
        if (useProxy) {
            options.addArguments("--proxy-server=http://ProxyHost:8080");
        }

        WebDriver driver = new ChromeDriver(options);

请查看https://peter.sh/experiments/chromium-command-line-switches/以获取更多 Chrome 开关信息。


0

设置代理服务器的另一种方法:

    Proxy proxy = new Proxy();
    proxy.setHttpProxy("<HOST:PORT>");
    proxy.setSslProxy("<HOST:PORT>");

    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.setCapability("proxy", proxy);

    WebDriver webDriver = new ChromeDriver(chromeOptions);

0

唯一使用代理的方式是这样的

String proxy="proxy-server-ip";
WebDriverManager.chromedriver().proxy(proxy).setup();

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