如何使用 Java 代码在 selenium 中为 Chrome 浏览器设置代理 [英] How to set proxy for Chrome browser in selenium using Java code

查看:70
本文介绍了如何使用 Java 代码在 selenium 中为 Chrome 浏览器设置代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行我的 selenium java 代码来测试网页.但由于网络限制,网页无法加载.当我手动设置代理并在浏览器中点击 url 时,它工作正常.现在我需要在运行 selenium 代码时传递这些代理设置.请帮我解决这个问题.

I am trying to run my selenium java code to test a webpage. But webpage is not loading because of network restrictions. When I set the proxy manually and hit the url in browser it works fine. Now I need to pass those proxy setting while running the selenium code. Please help me on this.

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

I tried below code, but still it shows the same error:

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);

推荐答案

问题已通过以下代码解决 -

Issue is resolved with below code -

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);

这篇关于如何使用 Java 代码在 selenium 中为 Chrome 浏览器设置代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆