Question: 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:

```java
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?

Answer: 
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();

```
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](https://stackoverflow.com/questions/48984806/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();

```