Reputation: 1017
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?
Upvotes: 1
Views: 7792
Reputation: 94
Using java.awt.Robot class comes with two main drawbacks.
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).
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();
Upvotes: 0
Reputation: 7
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);
Upvotes: 0
Reputation: 37826
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();
Upvotes: 0
Reputation: 11
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!
Upvotes: 0
Reputation: 1175
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();
Upvotes: 2