Entering Username/Password in Authentication pop-up window

2.7k views Asked by At

When I launch a website from Eclipse (selenium) it pops up a authentication box as below:

enter image description here

Here I am unable to enter Username and password, here are the things I tried:
1) Switching of Handle to pop up and identifying Xpath
2) Sending Username and Password in the URL (How to handle login pop up window using Selenium WebDriver?)
3) Sikuli (but requires image capture when executed in different system)
4) Using Robot function

        Robot rb = new Robot();
        StringSelection username = new StringSelection("XXXXX");
        System.out.println("Entering username");
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(username, null);            
        rb.keyPress(KeyEvent.VK_CONTROL);
        rb.keyPress(KeyEvent.VK_V);
        rb.keyRelease(KeyEvent.VK_V);
        rb.keyRelease(KeyEvent.VK_CONTROL);

        //tab to password entry field
        rb.keyPress(KeyEvent.VK_TAB);
        rb.keyRelease(KeyEvent.VK_TAB);
        Thread.sleep(2000);

        //Enter password by ctrl-v
        StringSelection pwd = new StringSelection("YYYYYYY");
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(pwd, null);
        rb.keyPress(KeyEvent.VK_CONTROL);
        rb.keyPress(KeyEvent.VK_V);
        rb.keyRelease(KeyEvent.VK_V);
        rb.keyRelease(KeyEvent.VK_CONTROL);

        //press enter
        rb.keyPress(KeyEvent.VK_ENTER);
        rb.keyRelease(KeyEvent.VK_ENTER);

None of the above worked till now, just after reaching the website (driver.get(URL)) the control does not seem to come back to eclipse

2

There are 2 answers

1
Vikas Ojha On

Use AutoIT scripts to fill that windows. That is the only way to elegantly handle this issue in Windows with Selenium. Check Handle Windows based Authentication Pop Up in Selenium using AutoIt at http://www.toolsqa.com/selenium-webdriver/autoit-selenium-webdriver/

0
Ajay Polsani On

The below code should work, it can be refactored :)

driver.navigate().to("url");
StringSelection selection = new StringSelection("username");
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection);
Thread.sleep(5000);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.delay(2000);
selection = new StringSelection("password");
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.delay(1000);
robot.keyPress(KeyEvent.VK_ENTER);  
robot.keyRelease(KeyEvent.VK_ENTER);