How to use tab key (button) in selenium WebDriver?

There are multiple ways to perform Tab key functionality. Using Actions Class, Using SendKeys() method & few other. Let us talk about them one by one.

TAB Key using Actions Class in Selenium WebDriver

1. Using function sendKeys(Keys.Tab).

    WebElement inputField = driver.findElement(By.Locator("LocatorValue"));
    inputField.sendKeys(Keys.TAB);

2. If you are using Java then you can use Java ROBOT class to perform keyboard actions.

Robot robot = new Robot();
// Simulate key Events
robot.keyPress(keyEvent.VK_TAB);
robot.keyRelease(keyEvent.VK_TAB);

3. You can use Ascii value in send key function like sendKeys(//0061); I am not sure the ASCII value of Tag it’s just an example.

4. Using Actions class. –

Basically, you need to have instance of Actions class and then call sendKeys with TAB as TAB key.
package learnAboutActionsClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class TabKeyUsingActionsClass {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
WebDriver driver = new FirefoxDriver();
  
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();
  
  driver.navigate().to("http://automationtalks.com/");
  
  WebElement element = driver.findElement(By.xpath("//input[@class='search']"));
  element.sendKeys("text to be searched");
   
   Actions act = new Actions(driver);
   
   act.sendKeys(Keys.TAB).build().perform();
   act.sendKeys(Keys.RETURN).build().perform();
   
 }

}

Hope This Helps !!!!

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories