1

I am using selenium to navigate to a page and take screenshots using Internet Explorer. But the problem is the login is taken care by a Javascript alert box. Now Selenium has a facility where the focus can be brought to the alert box by using Alert element and I have managed to bring the focus and also enter some values in the user name text box.

The problem is Selenium does not switch focus to the password text box and enters the username and password in the same box. I tried Java AWT Robot to click on the tab key and it changes the focus, but Selenium does not recognized this and it continues entering the user name and password in the same box.

Below is my code:

Robot robot = new Robot();
driver.get("the url");
Alert alert = driver.switchTo().alert();
alert.sendKeys("username");
robot.keyPress(KeyEvent.VK_TAB);
alert.sendKeys("password");
alert.accept();

What am I missing here? Is my approach here correct or do I have to take a different route?

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Madusudanan
  • 1,017
  • 1
  • 15
  • 36
  • On a Mac, Java app takes away the focus and I experienced the same. See below answer using Keys.TAB.toString() as a part of username and password. – Changgull Song Apr 24 '19 at 21:57

5 Answers5

2

hi Madusudanan Try the code by commenting the another switch method.

Robot robot = new Robot();
Alert alert=dr.switchTo().alert();
dr.get("the url");
alert.sendKeys("username");
//dr.switchTo().alert();
robot.keyPress(KeyEvent.VK_TAB);
alert.sendKeys("password");
alert.accept();
selva
  • 1,175
  • 1
  • 19
  • 39
  • Hi soft,I have updated my code.please check, I accidentally copied it twice.The focus is made by AWT i.e it is switching to the password textbox but it is not being recognized by selenium, thats the problem here. – Madusudanan Mar 21 '13 at 10:12
  • hi Madusudanan use this StringSelection stringSelection = new StringSelection("password"); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null); Robot robot = new Robot(); Alert alert=dr.switchTo().alert(); dr.get("the url"); alert.sendKeys("username"); //dr.switchTo().alert(); robot.keyPress(KeyEvent.VK_TAB); robot.keyPress(KeyEvent.VK_CONTROL); // Copy robot.keyPress(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_CONTROL); alert.accept(); – selva Mar 21 '13 at 12:04
  • Its working.I added a little more robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); which actually pressed the ok button on the pop up.Thanks so much for your help :). – Madusudanan Mar 21 '13 at 12:31
0

Not a Java answer but since I found this question searching for a .net answer to this problem.

If you're using .NET you'll need to use SendKeys rather than Robot

using System.Windows.Forms;

        _driver.SwitchTo().Alert().SendKeys("Username");
        SendKeys.SendWait("{TAB}");
        SendKeys.SendWait("password");
        SendKeys.SendWait("{Enter}");

Hope this helps someone!

Carl J.
  • 11
  • 1
0

According to your question, after focusing on Password field Selenium WebDriver failed to enter/input/type it's corresponding value. You can do type the password value by using Robot class. The following is the whole code:

//First write a method to use StringSelection
public void setClipboardData(String string) {
            StringSelection stringSelection = new StringSelection(string);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
    }

Robot robot = new Robot();
driver.get("Your URL");
Alert alert = driver.switchTo().alert();
alert.sendKeys("username");
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
//call setClipboardData method declared above
setClipboardData("Your Password");
//Copy Paste by using Robot class
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
alert.accept();
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
0

I create code like that from any source and work after add: a.keyRelease(KeyEvent.VK_ADD);

          // Initialize InternetExplorerDriver Instance.
          WebDriver driver = new InternetExplorerDriver();

          // Load sample calc test URL.
          driver.get("your homepage testing");

          //Code to handle Basic Browser Authentication in Selenium.
          Alert aa = driver.switchTo().alert();

          Robot a = new Robot();
          aa.sendKeys("beyond"+"\\"+"DND");  

          a.keyPress(KeyEvent.VK_TAB);
          a.keyRelease(KeyEvent.VK_TAB);
          a.keyRelease(KeyEvent.VK_ADD);                

          setClipboardData("P@ssw0rd");
          a.keyPress(KeyEvent.VK_CONTROL);
          a.keyPress(KeyEvent.VK_V);
          a.keyRelease(KeyEvent.VK_V);
          a.keyRelease(KeyEvent.VK_CONTROL);
          aa.accept();    private static void setClipboardData(String string)StringSelection stringSelection = new StringSelection(string);

Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);

Shadi
  • 7
  • 1
0

Using java.awt.Robot class comes with two main drawbacks.

  1. Mac issue: On a Mac OS, when we instantiate a Robot class, Java app launches in the background and takes away the focus (so some in the community were sending VK_META (Cmd key) and VK_TAB to get back to the alert). Since copy & paste shortcut keys are different for Mac, that needs to be also handled (using VK_META in place of VK_CONTROL).

  2. Jenkins or remote runner issue: Even if we accommodated the issue above, eventually when tests are run from Jenkins job, Robot instantiation becomes a problem again (Robotframework selenium execution through jenkins not working)

Fortunately, sendKeys can handle the tab key (as a scan code) and the code below worked for me.

Alert alert = driver.switchTo().alert();
alert.sendKeys("username" + Keys.TAB.toString() + "password");
alert.accept();