java - 如何在 Chrome 浏览器的 selenium Webdriver 中设置代理身份验证

标签 java selenium selenium-webdriver webdriver

我正在尝试自动化 Web 应用程序 selenium 2.0 [webdriver+java]。该 Web 应用程序当前部署在我们本地网络上的 UAT 服务器中。我的测试用例正在执行,但我必须手动输入代理身份验证测试执行开始时我的 Chrome 实例的详细信息。我已经尝试了堆栈溢出提供的所有解决方案,但仍然弹出身份验证消息。

enter image description here

这是我在驱动程序初始化过程中使用的代码

包 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();
            }
        }
    }

谁能给我一个解决方案,如何从应用程序本身给这个代理用户名和密码,而不是在弹出窗口(身份验证)上手动输入详细信息,任何帮助将不胜感激。谢谢

当前回答的仅针对

Selenium 3.4 仍处于测试阶段 目前仅针对 InternetExplorerDriver 实现

我使用 selenium 3.0 和 Google Chrome 作为我的网络浏览器。

最佳答案

你可以通过 MultiPass for HTTP basic authentication


下载扩展 https://chrome.google.com/webstore/detail/multipass-for-http-basic/enhldmjbphoeibbpdhmjkchohnidgnah

将扩展下载为 crx。您可以从 chrome-extension-downloader 获取它作为 crx

之后配置就很简单了。

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

我用过 sample site用于测试。

在配置Auth函数中提供你的url、用户名和密码并尝试

关于java - 如何在 Chrome 浏览器的 selenium Webdriver 中设置代理身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44940815/

相关文章:

ruby - 如果找不到元素,则继续执行脚本

ruby - 使用 Ruby selenium 打开浏览器选项卡

selenium - Webdriver 从 bamboo agent 得到错误的解决方案

java - 为什么 getText() 无法在 Struts 2 中格式化 double 值

java - Collectors toMap() - 从 Map.Entry 到 Map 结构的值映射器

java - OkHttp 缓存如何工作?

java - 在 Java 中使用 Selenium Webdriver 获取文本

java - 关于 LeetCode 279. 完美正方形的问题

javascript - 如何在 Protractor 中等待元素被启用

java - 使用 selenium 和 firefox 检查 highcharts 值