How to Press Shift+Tab Key Using Selenium Java

Profile picture for user arilio666

Shift+Tab key generally goes to the end of the selection area within the webpage, and this is what we will be discussing in this article on how we can perform this key press in selenium using java.

We can do this by using the actions class.

  • Consider this Facebook signup page where when we press the shift and tab keys together, it goes to the signup button, which is at the end and highlights it.
  • Let us check this out with our code.
        WebDriverManager.chromedriver().setup();
        ChromeDriver driver = new ChromeDriver();
        driver.get(
                "https://www.facebook.com/?stype=lo&jlou=Afdsf3w_2f4nh64NObHUvBnfPrM4fT6bqZce2a1kcPaWpcpp3SqK6dxEY_sttFMh0e9CEJhB3SWndtdhKHhaLlRZ82VQ0tU-iDMHwOUzHVcL7w&smuh=13929&lh=Ac_VtLdxnx_t5x6nK08");
        driver.manage().window().maximize();
        driver.findElement(By.xpath("//a[@class='_42ft _4jy0 _6lti _4jy6 _4jy2 selected _51sy']")).click();
        Thread.sleep(3000);
        Actions actions = new Actions(driver);
        actions.keyDown(Keys.SHIFT).sendKeys(Keys.TAB).keyUp(Keys.SHIFT).build().perform();
        
  • Using the actions class, we are invoking the key down and up event and using send keys to mimic the tab key as a combination.
  • This will make the web driver press the shift+tab keys together, highlighting the signup button.