Friday, 14 February 2014

How to press tab key in selenium webdriver in Java?

We can press TAB key or any other key in Selenium Web driver by 2 ways in Java.

  1. Using Keys.TAB
  2. Using Actions
Using Keys.TAB
To use this method, you will have to import below class.
import org.openqa.selenium.Keys;

driver.findElement(By.name("name")).sendKeys(Keys.TAB);

Using Actions Class

To use this method, you will have to import below classes.
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

Actions action = new Actions(driver); 
action.sendKeys(Keys.TAB).bui‌​ld().perform();

Complete Example in Java
package temp;
import java.io.File;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.Keys;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

@SuppressWarnings("unused")

public class SendKey {

public static void main(String [] arg) 

{
System.setProperty("webdriver.chrome.driver", "C:\\Selenuim\\chromedriver2.8.exe");
WebDriver driver = new ChromeDriver();

try{


driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

driver.manage().timeouts().pageLoadTimeout(50,TimeUnit.SECONDS);

driver.get("https://www.google.co.in/preferences");

driver.manage().window().maximize();

//driver.get("http://register.rediff.com/register/register.php");

Thread.sleep(2000);

WebElement e = driver.findElement(By.xpath("//div[@id='instant-radio']/div[3]/span"));
//using Keys.TAB
e.sendKeys(Keys.TAB);

//using Actions

Actions action = new Actions(driver);

action.sendKeys(e,Keys.TAB).build().perform();
}

catch(Exception e){

System.out.println("Exception - > " + e.toString());

}

finally{

driver.close();
driver.quit();

}

} //main function ends

}//class ends

Please note that we can also press any other key like ENTER, F1, F2, HOME, ARROW_UP, ESCAPE in same fashion.

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

6 comments:

  1. Too good explanation and very clean code ! Thank you !

    ReplyDelete
  2. Really good explanantion. Thanks!

    ReplyDelete
  3. Hi Sagar,

    Can you let me know how to handle the java applet window which says,

    do you want to continue the connection to this website is untrusted in Internet explorer using webdriver java.
    By default the cancel button is highlighted in applet window. I just want to click continue button.
    Thanks in advance.





    ReplyDelete
    Replies
    1. Hi Tamil,
      You can not handle Applet elements using Selenium.
      You will have to use AutoIT or Ranorex or LeanFT for that.

      Delete
    2. I tried using Robot class to tab to continue button and click it. It works only couple of times but not consistent. I even used thread. sleep but I still face synchronization issues.
      I tried using Autoit but it does not identify the class and instance. I guess autoit workd only for windows dialog not sure though.

      Thanks

      Delete
    3. Can you try pressing key on Applet element. I do not know of any other solution!

      Delete

Buy Best Selenium Books

Contributors