Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java代码在selenium中为Chrome浏览器设置代理_Java_Selenium_Proxy - Fatal编程技术网

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

如何使用Java代码在selenium中为Chrome浏览器设置代理,java,selenium,proxy,Java,Selenium,Proxy,我正在尝试运行selenium java代码来测试网页。但由于网络限制,网页未加载。当我手动设置代理并在浏览器中点击url时,效果很好。现在,我需要在运行selenium代码时传递这些代理设置。请帮我做这个 我尝试了下面的代码,但仍然显示相同的错误: Proxy p=new Proxy(); // Set HTTP Port to 7777 p.setHttpProxy("www.abc.com:8080"); // Create desired Capability object Des

我正在尝试运行selenium java代码来测试网页。但由于网络限制,网页未加载。当我手动设置代理并在浏览器中点击url时,效果很好。现在,我需要在运行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);

问题通过以下代码解决-

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

不推荐将功能对象传递给ChromeDriver()构造函数。使用代理的一种方法是:

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

另一种方法是:

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

有关更多Chrome开关的信息,请参见将功能对象传递给ChromeDriver()构造函数已被弃用。你可以找到新的官方文件


嗨,巴尼,谢谢你的回复。但当我尝试实现上述代码时,仍然显示出相同的错误——“无法访问此网站”将功能对象传递给ChromeDriver()构造函数是不推荐的。在selenium 4@Barney中尝试过,但ip地址不正确,出于某种原因,它仍然访问网站:(.Hi@Praveen Medipally,我得到“无法访问此网站”上面的代码在浏览器中出错。我有我的代理服务器的用户id和密码。我只是像这样将其插入我的url中。上面的代码是否仍然适用于您?不推荐将功能对象传递给ChromeDriver()构造函数。将功能对象传递给ChromeDriver())构造函数已弃用。已在selenium 4中尝试,并故意提供了错误的代理,但它仍连接到网站:(。
        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);
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);