Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to press "TAB" then "ENTER" key in Selenium WebDriver using Java?

I am doing automation testing using java with Selenium WebDriver. I want to click on tabs. I would like to check tab functionality.

I can use Tab key to get the button as below:

WebElement webElement = driver.findElementByXPath("");
webElement.sendKeys(Keys.TAB);
webElement.sendKeys(Keys.ENTER);

I have a form with multiple fields I want to track upon pressing key tab key is my control moving to next field successfully or not. Also I want to check upon which my control is below is my form image

But how do I click one by one tab. Basically I need to achieve press Tab key and then press Enter key to click the button.

I learning selenium. Please help me. Thanks in advance.

like image 866
Deepak gupta Avatar asked Apr 21 '15 09:04

Deepak gupta


People also ask

How can we press the Enter key from the keyboard in Selenium?

We can type Enter/Return key in Selenium. We shall use the sendKeys method and pass Keys. ENTER as an argument to the method. Also, we can use pass Keys.

How do you hit a tab in Selenium?

webElement. sendKeys(Keys. TAB); //This will enter the string which you want to pass and will press "Tab" button .

Can Selenium WebDriver press keyboard keys?

Now, as we discussed, Selenium WebDriver provides two ways to send any keyboard event to a web element: sendKeys() method of WebElement class. Actions class.


2 Answers

Please see the solution that works with my example form

FormTab.html:

<!DOCTYPE html>
<html>
<body>
<form>
    First name:<br>
    <input type="text" name="firstname" value="Mickey">
    <br>
    Last name:<br>
    <input type="text" name="lastname" value="Mouse">
    <br><br>
    <input type="submit" name="submit" value="Submit">
</form>
<p>If you click "Submit", nothing happens.</p>
</body>
</html>

Java code:

WebDriver driver = new FirefoxDriver();

//Insert path to your file
driver.get("FormTab.html");

//Three example elements
WebElement firstField = driver.findElement(By.name("firstname"));
WebElement secondField = driver.findElement(By.name("lastname"));
WebElement submit = driver.findElement(By.name("submit"));

//Start with the first field
firstField.sendKeys();
//Verify that we in the first field
if(driver.switchTo().activeElement().equals(firstField))
    System.out.println("First element is in a focus");
else
    //Add Assertion here - stop execution
    System.out.println("ASSERTION - first element not in the focus");

firstField.sendKeys(Keys.TAB);

//Verify that we in the second field
if(driver.switchTo().activeElement().equals(secondField))
    System.out.println("Second element is in a focus");
else
    //Add Assertion here - stop execution
    System.out.println("ASSERTION - second element not in the focus");

secondField.sendKeys(Keys.TAB);

if(driver.switchTo().activeElement().equals(submit))
    System.out.println("Submit element is in a focus");
else
    //Add Assertion here - stop execution
    System.out.println("ASSERTION - submit element not in the focus");

//Click the button 
submit.click();

//Need be closed also in case the assertion - use @After
driver.close();
like image 102
Eugene Avatar answered Oct 25 '22 13:10

Eugene


Try the below code.This is working fine.

        Actions builder = new Actions(driver);         
        builder.SendKeys(OpenQA.Selenium.Keys.Tab).Build().Perform();
        builder.Release().Perform();            
        builder.SendKeys(OpenQA.Selenium.Keys.Tab).Build().Perform();
        builder.Release().Perform();
like image 38
SaiPawan Avatar answered Oct 25 '22 15:10

SaiPawan