java - Firefox 身份验证弹出窗口出现错误 : User prompt of type promptUserAndPass is not supported

标签 java selenium-webdriver selenium-firefoxdriver browsermob-proxy

我的身份验证功能在 chrome 中运行良好,但由于 Firefox 处理方式不同,我无论如何都无法登录身份验证。

我的 Firefox 版本:72.02

我发现它是已知的错误,但它们是 1 年前的帖子,如果有人有解决方案,请分享

https://bugzilla.mozilla.org/show_bug.cgi?id=1556026

https://bugzilla.mozilla.org/show_bug.cgi?id=1556307

正常代码将无法如下工作,因为我需要将 token 添加到 url 运行时,如 chrome 中所示但不是 Firefox 中所示

driver.get("https://your-username:your-password@example.com");

所以我需要在这种情况下做一些 sendKeys,这对我来说失败了

我收到错误:

Exception in thread "main" org.openqa.selenium.UnsupportedCommandException: User prompt of type promptUserAndPass is not supported Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03' System info: host: '', ip: '', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_66' Driver info: org.openqa.selenium.firefox.FirefoxDriver



我正在尝试的代码:
public static void main(String[] args) throws InterruptedException, AWTException {

       /* BrowserMobProxy proxy = new BrowserMobProxyServer();
        proxy.start(0);
        Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);*/

        // put our custom header to each request
/*       proxy.addRequestFilter((request, contents, messageInfo)->{
            request.headers().add("my-test-header", "my-test-value");
            System.out.println(request.headers().entries().toString());
            return null;
        });*/

        // Setting up Proxy for chrome
      //  ChromeOptions opts = new ChromeOptions();




        WebDriver driver = null;
        String url = "https://qa.xyz-domain/new-request/";
        String UserName = "xyzuser";
        String Password = "xyz@202";

        System.setProperty("webdriver.gecko.driver",
                "D:\\Demoproject\\src\\main\\resources\\drivers\\geckodriver.exe");
        FirefoxOptions options = new FirefoxOptions();
         options.setCapability("network.http.phishy-userpass-length", 255);
         options.setCapability("network.automatic-ntlm-auth.trusted-uris","testcloud");
        // options.setp
    //   options.setCapability(CapabilityType.);


         String val=UserName+":"+Password;

         String encodedCreadentials = "Basic " + (Base64.getEncoder().encodeToString(val.getBytes()));

         options.setCapability("Authorization",encodedCreadentials);

        // String proxyOption = "--proxy-server=" + seleniumProxy.getHttpProxy();
       //  options.addArguments(proxyOption);
        // options.addArguments(arguments)
        driver = new FirefoxDriver(options);

        driver.get(url);
        Thread.sleep(5000);
        driver.findElement(By.name("loginfmt")).sendKeys("user.name@xyz.com");
        driver.findElement(By.name("loginfmt")).sendKeys(Keys.ENTER);
        Thread.sleep(15000);
        driver.switchTo().alert().sendKeys(UserName);
        /*BrowserMobProxy browserMobProxy = new BrowserMobProxyServer();


        browserMobProxy.addHeader("Authorization", encodedCreadentials);
        browserMobProxy.start();*/


        //ProxyServer bmp = new ProxyServer(4444);
    /*  Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_ENTER);
        driver.switchTo().alert().sendKeys(UserName);*/


/*      Alert alert = driver.switchTo().alert();

        System.out.println(alert.getText());

        if (alert.getText().contains("jjjj")) {*/
    //      alert.sendKeys("UserName");
            //driver.switchTo().alert().dismiss();;
            //driver.switchTo().alert().sendKeys(UserName + Keys.TAB + Password);
            //alert.sendKeys(UserName + Keys.TAB + Password);

            /*String url2 = driver.getCurrentUrl().replaceAll("https://", "");
            System.out.println("url2 = " + url2);

            String url3 = "https://" + UserName + ":" + Password + "@" + url2 + "/";
            System.out.println("url3 = " + url3);
            driver.get(url3);

            //driver.switchTo().alert().sendKeys(UserName + Keys.TAB + Password);
            //driver.switchTo().alert().accept();

            String url2 = driver.getCurrentUrl().replaceAll("https://", "");
            System.out.println("url2 = " + url2);

            String url3 = "https://" + UserName + ":" + Password + "@" + url2 + "/";
            System.out.println("url3 = " + url3);
            driver.get(url3);*/

        /*} else {
            System.out.println("not appearing");
        }*/



    }

}

我尝试使用 BrowserMobProxy,但我相信它仅适用于 http 请求,我猜想和 https har 需要,如果我的理解有问题,请告诉我。或者我们无论如何都可以将它用于 https

引用:

enter image description here

最佳答案

由于缺少规范,他们似乎还没有开始解决这个问题。
但是,我找到了自动绕过此验证的技巧。
您需要导入 threading , win32api , win32con .
首先使用第一个线程启动浏览器,打开要操作的页面。
当出现输入用户和密码的输入框时,按住页面同时启动另一个线程。
在第二个线程中,调用Python键盘事件,通过tab键输入用户名和密码回车。
例子:

    def input_auth(self):
        time.sleep(10)
        for i in 'account':
            type_key(i)
        type_key('TAB')
        for i in 'password':
            type_key(i)
        type_key('ENTER')

    def get_url(self, driver, url):
        driver.get(url)
     
    def type_key(self, key):
        win32api.keybd_event(int(self.keyboard[key]),0,0,0)
        win32api.keybd_event(int(self.keyboard[key]),0,win32con.KEYEVENTF_KEYUP,0)
    
    def run(self,driver,url):
        thread_get_url = threading.Thread(target=self.get_url,args=[driver,url])
        thread_input_auth= threading.Thread(target=self.input_auth)
        thread_get_url .start()
        thread_input_auth.start()
        thread_get_url .join()
        thread_input_auth.join()
引用:https://blog.csdn.net/qq_35741999/article/details/84670673

关于java - Firefox 身份验证弹出窗口出现错误 : User prompt of type promptUserAndPass is not supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60172909/

相关文章:

python - Selenium-调试 : Element is not clickable at point (X, Y)

java - 从android中的数组列表中逐一使用项目

Java调用另一个类中的方法

java - 将 FTP 代理与 apache commons-net 一起使用

linux - 我们可以从 Linux 终端运行 Selenium WebDriver 代码的 Jar 文件吗?如何?

java - Firefox 驱动程序失败并出现无法访问的错误

java - 你如何在 quickfixj 消息中获得重复组

java - Junit 5,Spring 应用程序上下文未在 @DirtiesContext 上关闭

java - 在java中切片列表

python-3.x - 无法使用 selenium 4 的 option.set_preference 在 python 中加载现有的 firefox 配置文件