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
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.
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.
webElement. sendKeys(Keys. TAB); //This will enter the string which you want to pass and will press "Tab" button .
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.
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();
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With