【问题标题】:set proxy authentication for firefox using selenium使用 selenium 为 firefox 设置代理身份验证
【发布时间】:2019-11-06 08:31:35
【问题描述】:

我想在 java 中使用 selenium 在 firefox 中设置代理用户名和密码。这是我的代码:

public static void main(String[] args) {
        System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
        WebDriver driver = null;
        try {
            String PROXY_HOST = "**********";
            int PROXY_PORT = ****;
            String USERNAME = "******";
            String PASSWORD = "******";
            ProfilesIni profileIni = new ProfilesIni();
            FirefoxProfile profile = profileIni.getProfile("test");
            profile.setPreference("network.proxy.type", 1);
            profile.setPreference("network.proxy.http", PROXY_HOST);
            profile.setPreference("network.proxy.http_port", PROXY_PORT);
            profile.setPreference("network.proxy.ssl", PROXY_HOST);
            profile.setPreference("network.proxy.ssl_port", PROXY_PORT);
            FirefoxOptions options = new FirefoxOptions();
            options.setProfile(profile);
            driver = new FirefoxDriver(options);
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            WebDriverWait wait = new WebDriverWait(driver, 10); 
            String parentWindowHandler = driver.getWindowHandle();
            for (String currentWindow: driver.getWindowHandles())
                   driver.switchTo().window(currentWindow);
            Thread.sleep(1000);
            autoType(USERNAME);
            Thread.sleep(1000);
            autoTab();
            Thread.sleep(1000);
            autoType(PASSWORD);
            Thread.sleep(1000);
            autoSubmit();
            Thread.sleep(1000);
            driver.switchTo().window(parentWindowHandler);
            Thread.sleep(1000);
            driver.get("http://ident.me");
        } catch (Exception e2) {
            System.out.println("Error: " + e2);
        }finally {
        if(driver != null)
            driver.close();
        }
    }

    private static void autoType(String string) {
        Robot robot;
        try {
            robot = new Robot();
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            StringSelection stringSelection = new StringSelection(string);
            clipboard.setContents(stringSelection, null);
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);
        } catch (AWTException e) {
            System.out.println("autoType Error: " + e);
        }
    }

    private static void autoTab() {
        Robot robot;
        try {
            robot = new Robot();
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);
        } catch (AWTException e) {
            System.out.println("autoTab Error: " + e);
        }
    }

    private static void autoSubmit() {
        Robot robot;
        try {
            robot = new Robot();
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
        } catch (AWTException e) {
            System.out.println("autoSubmit Error: " + e);
        }
    }

代码加载 firefox 配置文件并设置代理主机和代理端口。然后它会创建 firefox 驱动程序并打开新的 firefox 窗口。当身份验证提示打开时,它输入用户名和密码并提交它们,但它无法加载 url 并在下面抛出错误:

Error: org.openqa.selenium.WebDriverException: TypeError: this.tabModal is null

【问题讨论】:

  • 如果您使用的是基本身份验证(我假设您是),您可以尝试在所有请求中设置标头。我在 Selenium git 的另一个线程中回答了这个问题:github.com/SeleniumHQ/selenium/issues/1802

标签: java selenium firefox proxy


【解决方案1】:

简短的回答是你不能轻易做到(有时根本不可能)。有大量的工作,但我尝试过的每个人都只能在部分时间工作,或者需要对您要添加的每个新浏览器进行大量配置。

我发现唯一可以始终如一地工作的解决方案是使用 Luminati 代理管理器 (LPM)。它允许您使用他们的开源软件在本地网络上打开受密码保护的代理。

设置一个单独的东西似乎是一个巨大的痛苦,但这是我始终如一地工作的唯一方法。

【讨论】:

    猜你喜欢
    • 2016-10-13
    • 2012-08-24
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 2019-01-05
    • 2021-10-07
    • 2022-08-19
    • 2013-10-18
    相关资源
    最近更新 更多