简体   繁体   English

如何在 Chrome 浏览器的 seleniumWebdriver 中设置代理身份验证

[英]How to set Proxy Authentication in seleniumWebdriver for Chrome Browser

I'm trying to Automate a web application selenium 2.0 [webdriver+java].The web application is currently deployed in our UAT servers on our local network.My test cases are executing, but I have to manually enter the Proxy Authentication details for my Chrome instance at the start of the test execution.我正在尝试自动化 Web 应用程序 selenium 2.0 [webdriver+java]。该 Web 应用程序当前部署在我们本地网络上的 UAT 服务器中。我的测试用例正在执行,但我必须手动输入我的代理身份验证详细信息测试执行开始时的 Chrome 实例。 I have tried all the solutions provided on stack overflow but still, the authentication message pops out.我已经尝试了堆栈溢出提供的所有解决方案,但仍然弹出身份验证消息。

在此处输入图像描述

This is the code I'm using in my driver initializing process这是我在驱动程序初始化过程中使用的代码

package com.misyn.ess.ui;包 com.misyn.ess.ui;

import java.util.Arrays;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

/**
 *
 * @author User
 */
public class DriverClass {

    private String baseUrl;
    private String driverPath;
    private String driverName;
    private static WebDriver driver;
    private static DriverClass driverClass;

        private DriverClass() {
            try {
                baseUrl = "http://192.168.0.10:8282/ess";
                driverPath = "E:\\Work_Folder\\SelTools\\chromedriver.exe";
                driverName = "webdriver.chrome.driver";

                //Set the location of the ChromeDriver
            System.setProperty(driverName, driverPath);
            //Create a new desired capability
            DesiredCapabilities capabilities = DesiredCapabilities.chrome();
            // Create a new proxy object and set the proxy
            Proxy proxy = new Proxy();
            proxy.setHttpProxy("192.168.0.200:3128");
            proxy.setSocksUsername("avishka");
            proxy.setSocksPassword("12345678");
            //Add the proxy to our capabilities 
            capabilities.setCapability("proxy", proxy);
            //Start a new ChromeDriver using the capabilities object we created and added the proxy to
            driver = new ChromeDriver(capabilities);

            //Navigation to a url and a look at the traffic logged in fiddler
            driver.navigate().to(baseUrl);


    //            System.setProperty(driverName, driverPath);
    //            driver = new ChromeDriver();
    //            driver.get(baseUrl);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

Can anyone give me a solution how to give this proxy username and password thing from the application itself than manually entering details on the pop-up(Authentication), any help would be much appreciated.Thanks任何人都可以给我一个解决方案,如何从应用程序本身提供这个代理用户名和密码,而不是在弹出窗口(身份验证)上手动输入详细信息,任何帮助将不胜感激。谢谢

the currently answered one is only for当前回答的仅适用于

As of Selenium 3.4 it is still in beta Right now implementation is only done for InternetExplorerDriver从 Selenium 3.4 开始,它仍处于测试阶段 目前仅针对 InternetExplorerDriver 实现

Where I'm using selenium 3.0 and Google Chrome as my web browser.我在哪里使用 selenium 3.0 和 Google Chrome 作为我的网络浏览器。

You can do via MultiPass for HTTP basic authentication您可以通过MultiPass 进行 HTTP 基本身份验证

Download the extension from从下载扩展
https://chrome.google.com/webstore/detail/multipass-for-http-basic/enhldmjbphoeibbpdhmjkchohnidgnah https://chrome.google.com/webstore/detail/multipass-for-http-basic/enhldmjbphoeibbpdhmjkchohnidgnah

Download the extension as crx.将扩展下载为 crx。 You can get it as crx from chrome-extension-downloader您可以从chrome-extension-downloader获取它作为 crx

After that the config is simple.之后配置就很简单了。

import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

/**
 *
 * @author Phystem
 */
public class ChromeAuthTest {

    WebDriver driver;

    public ChromeAuthTest() {
        System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
    }

    private void initDriver() {
        ChromeOptions cOptions = new ChromeOptions();
        cOptions.addExtensions(new File("MultiPass-for-HTTP-basic-authentication_v.crx"));
        driver = new ChromeDriver(cOptions);
        configureAuth(
                "https://the-internet.herokuapp.com/basic_auth",
                "admin",
                "admin");
    }

    private void configureAuth(String url, String username, String password) {
        driver.get("chrome-extension://enhldmjbphoeibbpdhmjkchohnidgnah/options.html");
        driver.findElement(By.id("url")).sendKeys(url);
        driver.findElement(By.id("username")).sendKeys(username);
        driver.findElement(By.id("password")).sendKeys(password);
        driver.findElement(By.className("credential-form-submit")).click();
    }

    public void doTest() {
        initDriver();
        driver.get("https://the-internet.herokuapp.com/basic_auth");
        System.out.println(driver.getTitle());
        driver.quit();
    }

    public static void main(String[] args) {
        new ChromeAuthTest().doTest();
    }
}

I have used a sample site for testing.我使用了一个示例站点进行测试。

Provide your url,username and password in the configure Auth function and try在配置 Auth 函数中提供您的 url、用户名和密码,然后尝试

    public class DriverClass {

    private String baseUrl;
    private String driverPath;
    private String driverName;
    private static WebDriver driver;
    private static DriverClass driverClass;

    public DriverClass() {
        try {
            baseUrl = "http://192.168.0.10:8282/ess";
            driverPath = "E:\\Work_Folder\\SelTools\\chromedriver.exe";
            driverName = "webdriver.chrome.driver";
            System.setProperty(driverName, driverPath);

            Proxy proxy = new org.openqa.selenium.Proxy();
            proxy.setSslProxy("192.168.0.200" + ":" + 3128);
            proxy.setFtpProxy("192.168.0.200" + ":" + 3128);
            proxy.setSocksUsername("avishka");
            proxy.setSocksPassword("12345678");

            DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();
            desiredCapabilities.setCapability(CapabilityType.PROXY, proxy);


            driver = new ChromeDriver(desiredCapabilities);


            driver.get(baseUrl);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The proxy setting has been added with desired capabilities to pass values to proxy authentication,worked finally代理设置已添加所需的功能,以将值传递给代理身份验证,最终起作用

This code (from Avishka Perera's answer) does not work for me:此代码(来自 Avishka Perera 的回答)对我不起作用:

    proxy.setSocksUsername("avishka");
    proxy.setSocksPassword("12345678");

The username and password set in this way do not take effect for the http/https proxy - the Proxy Authentication box still popped up.这样设置的用户名和密码对http/https代理不生效——仍然弹出Proxy Authentication框。

I'm using Selenium java 3.141.0, ChromeDriver 2.33 and chrome 70. What works for me is to follow Mike's answer here Selenium using Python: enter/provide http proxy password for firefox .我正在使用 Selenium java 3.141.0、ChromeDriver 2.33 和 chrome 70。对我有用的是按照 Mike 在此处使用 Python 的 Selenium 的回答:enter/provide http proxy password for firefox Create the zip file, then add the extension like this:创建 zip 文件,然后像这样添加扩展名:

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addExtensions(new File("src/test/resources/proxy.zip"));
WebDriver driver = new ChromeDriver(chromeOptions);

One catch is that the above code will run into error if you set "--headless" argument because chrome in headless mode cannot have extension ( Is it possible to run Google Chrome in headless mode with extensions? ).一个问题是,如果您设置“--headless”参数,上面的代码将会出错,因为无头模式下的 chrome 不能有扩展( Is it possible to run Google Chrome in headless mode with extensions? )。 If your Chrome runs in Docker container and cannot show the UI, then to get this solution work, you'll need to run with Xvfb instead of in headless mode.如果您的 Chrome 在 Docker 容器中运行并且无法显示 UI,那么要使此解决方案起作用,您需要使用 Xvfb 而不是无头模式运行。

Simple method to add authenticated proxy using selenium wire in Both firefox and chrome在 Firefox 和 Chrome 中使用硒线添加经过身份验证的代理的简单方法

In python在蟒蛇

Step:1步骤1

pip3 install selenium-wire

Step:2第2步

from seleniumwire import webdriver
from selenium import webdriver

step:3步骤:3

Add proxy in below-mensioned format以下面提到的格式添加代理

proxy= "username:password@ip:port"
        options = {'proxy': {'http': proxy, 'https': proxy, 'no_proxy': 'localhost,127.0.0.1,dev_server:8080'}}

step:4 pass proxy as an argument步骤:4将代理作为参数传递

CHROME铬合金

driver = webdriver.Chrome(options=chrome_options, executable_path="path of chrome driver", seleniumwire_options=options)

Firefox火狐

driver = webdriver.Firefox(seleniumwire_options=options, executable_path="path of firefox driver", options=firefox_options)

step:5 Verify proxy applied by requesting the url https://whatismyipaddress.com/步骤:5通过请求 url https://whatismyipaddress.com/验证应用的代理

time.sleep(20)
driver.get("https://whatismyipaddress.com/")

Note: But selenium log shows it runs in without proxy because we are using an external package to apply proxy.注意:但是 selenium 日志显示它在没有代理的情况下运行,因为我们正在使用外部包来应用代理。

I know this is an old thread, still leaving a solution which worked for me using browsermob proxy , for someone who still needs an option.我知道这是一个旧线程,仍然为仍然需要选项的人留下一个使用browsermob proxy对我有用的解决方案。

Maven dependency: Maven 依赖项:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.0.0</version>
</dependency>
<dependency>
    <groupId>net.lightbody.bmp</groupId>
    <artifactId>browsermob-core</artifactId>
    <version>2.1.5</version>
</dependency>

Java Code: Java代码:

// I am using firefox
System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\geckodriver.exe");

BrowserMobProxy browsermobProxy = new BrowserMobProxyServer();
browsermobProxy.setChainedProxy(new InetSocketAddress(PROXY_HOSTNAME, PROXY_PORT));
browsermobProxy.chainedProxyAuthorization(PROXY_USERNAME, PROXY_PASSWORD, AuthType.BASIC);
browsermobProxy.start(0);

FirefoxBinary firefoxBinary = new FirefoxBinary();
FirefoxOptions firefoxOptions = new FirefoxOptions();

firefoxOptions.setBinary(firefoxBinary );
firefoxOptions.setProfile(firefoxProfile);

firefoxOptions.setProxy(ClientUtil.createSeleniumProxy(browsermobProxy));

WebDriver webDriverWithProxy = new FirefoxDriver(firefoxOptionsWithProxy);

webDriverWithProxy.get("https://stackoverflow.com/");

The approach that worked perfectly fine for me is by using AutoIT.对我来说非常有效的方法是使用 AutoIT。

Install autoIT and prepare a simple script as shown in the picture attached and execute the script file from your testscript using Runtime.getRuntime().exec("\YOUR_SCRIPT.exe") before navigating to the baseURL.安装 autoIT 并准备一个简单的脚本,如附图所示,并在导航到 baseURL 之前使用 Runtime.getRuntime().exec("\YOUR_SCRIPT.exe") 从您的测试脚本执行脚本文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM