简体   繁体   English

如何在没有 autoit 和 url 方法的情况下使用 java 处理带有 selenium webdriver 的浏览器的身份验证警报

[英]How to handle authentication alert of browser with selenium webdriver using java without autoit and url method

I tried to set proxy with我试图设置代理

Proxy proxy = new Proxy();
proxy.setHttpProxy("http://MY_USERNAME:MY_PASSWORD@MY_HOST:MY_PORT");

It is redirecting to specific URL but it is not actually setting proxy and giving me local IP instead MY_HOST.它正在重定向到特定的 URL,但实际上并没有设置代理,而是给我本地 IP,而不是 MY_HOST。

I can not use autoit script.我不能使用 autoit 脚本。

Please guide me that how can I handle alert box请指导我如何处理警报框

enter image description here在此处输入图片说明

I have tried with driver.switchTo().alert();我试过driver.switchTo().alert(); but, the code is not working after the statement driver.get(MY_URL);但是,在语句driver.get(MY_URL);之后代码driver.get(MY_URL); when the popup appears.当弹出窗口出现时。

Note : Both options (1) by set crx file and (2) by giving user name and password in URL with host and port, are authenticating successfully but please note that it is not actually set proxy as required but instead it gives local IP注意:这两个选项(1)通过设置 crx 文件和(2)通过在带有主机和端口的 URL 中提供用户名和密码,都已成功进行身份验证,但请注意,它实际上并未按要求设置代理,而是提供了本地 IP

java.awt.Robot class can be used for authentication java.awt.Robot类可用于身份验证

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

login() throws Exception {
    // Pass username
    autoType(username);
    // to move to Password field
    autoTab();
    // Enter Password
    autoType(password);
    // To click on login
    autoSubmit();
}

private static void autoType(String string) throws AWTException {
    Robot 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);
}

private static void autoTab() throws AWTException {
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_TAB);
    robot.keyRelease(KeyEvent.VK_TAB);
}

private static void autoSubmit() throws AWTException {
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
}

You can create a Chrome extension that can handle the proxy on the fly.您可以创建一个可以即时处理代理的 Chrome 扩展程序。 ChromeDriver does not provide any capability to handle HTTP proxy that needs credentials. ChromeDriver 不提供任何处理需要凭据的 HTTP 代理的功能。

Create a zip file proxyExtension.zip that contains the following 2 files;创建一个包含以下 2 个文件的 zip 文件proxyExtension.zip

background.js背景.js

var config = {
    mode: "fixed_servers",
    rules: {
      singleProxy: {
        scheme: "http",
        host: "YOU_PROXY_ADDRESS",
        port: parseInt(YOUR_PROXY_PORT)
      },
      bypassList: ["foobar.com"]
    }
  };

chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

function callbackFn(details) {
    return {
        authCredentials: {
            username: "YOUR_PROXY_USERNAME",
            password: "YOUR_PROXY_PASSWORD"
        }
    };
}

chrome.webRequest.onAuthRequired.addListener(
        callbackFn,
        {urls: ["<all_urls>"]},
        ['blocking']
);

YOU_PROXY_ADDRESS , YOUR_PROXY_PORT , YOUR_PROXY_USERNAME , YOUR_PROXY_PASSWORD fields will be replaced with your informations. YOU_PROXY_ADDRESSYOUR_PROXY_PORTYOUR_PROXY_USERNAMEYOUR_PROXY_PASSWORD字段将替换为您的信息。

manifest.json清单文件

{
    "version": "1.0.0",
    "manifest_version": 2,
    "name": "Chrome Proxy",
    "permissions": [
        "proxy",
        "tabs",
        "unlimitedStorage",
        "storage",
        "<all_urls>",
        "webRequest",
        "webRequestBlocking"
    ],
    "background": {
        "scripts": ["background.js"]
    },
    "minimum_chrome_version":"22.0.0"
}

Then, initialize the webdriver with the following code;然后,使用以下代码初始化 webdriver;

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addExtensions(new File("path_to_extension_file/proxyExtension.zip"));
WebDriver driver = new ChromeDriver(chromeOptions);

Please change the path_to_extension_file to your directory that has the proxyExtension.zip file.请将path_to_extension_file更改为包含proxyExtension.zip文件的目录。

You can also find more information on the link .您还可以在 链接上找到更多信息。

暂无
暂无

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

相关问题 有没有任何方法可以使用 selenium 来处理身份验证警报,而无需任何操作系统工具,如 autoIT 和机器人 - Is there any way to handle authentication alert using selenium without any OS tool like autoIT and robot 如何使用 Selenium Webdriver 处理浏览器身份验证弹出窗口 - How to Handle Browser Authentication popup using Selenium Webdriver 如何使用 Java 处理 Selenium WebDriver 的身份验证弹出窗口 - How to handle authentication popup with Selenium WebDriver using Java 如何使用 Java 使用 Selenium WebDriver 在 Chrome 中处理身份验证弹出窗口 - How to handle authentication popup in Chrome with Selenium WebDriver using Java 如何使用Java中的Selenium WebDriver处理身份验证弹出窗口 - How to handle authentication popup with Selenium WebDriver in Java 如何在 Safari 浏览器中使用 selenium 处理基本的 url 身份验证 - How to handle basic url authentication using selenium in Safari browser 如何在不使用autoit且仅使用selenium和java的情况下上传文件? - How to upload a file without using autoit and with only using selenium and java? 如何在Selenium WebDriver中使用Java处理灯箱 - How to handle lightbox using java in selenium webdriver 如何使用 java 在 Selenium WebDriver 中处理 iframe - How to handle iframe in Selenium WebDriver using java 有没有办法在不使用 alert.dismiss 方法的情况下处理 java 基于脚本的弹出窗口 selenium (Java)? - Is there any way to handle java Script based Pop up in selenium (Java) without using alert.dismiss method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM