4

My Auth functionality is working great in chrome but as Firefox handle it differently I am not able to login the auth by anyway.

My Firefox version : 72.02

I have found that it is known bug but they are 1 year old post, if anyone has solution kindly share

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

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

Normal code will not work as below as I need to add token to url runtime as similar shown in chrome but not in Firefox

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

So I need to do some sendKeys in this case which is failing for me

I am getting error as :

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

Code I am trying:

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");
        }*/



    }

}

I tried to use BrowserMobProxy but I believe it works with http request only I guess and for https har required, let me know if there is issue with my understanding. or we can use it anyway for https

refer:

enter image description here

Shubham Jain
  • 13,158
  • 9
  • 60
  • 102

1 Answers1

0

It appears they did not start to fix this yet, due to missing specifications. However, I've found a trick to bypass this verification automatically.

You need to import threading, win32api, win32con.

First of all, use the first thread to start the browser, and open the page to be operated.

When the input box for entering the user and password is displayed, hold the page and start another thread at the same time.

In the second thread, the Python keyboard event is called, enter the username and the password through tab and enter.

Example:

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

Reference:https://blog.csdn.net/qq_35741999/article/details/84670673

Hao Feng
  • 31
  • 3