简体   繁体   中英

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 图片

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.

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();
Actions builder = new Actions(driver);
        Action enter= builder
                .keyDown(Keys.TAB)
                .build();
enter.perform();

 Action releaseEnter= builder
                .keyUp(Keys.ENTER)
                .build();
releaseEnter.perform();

当您在页面上时,您可以尝试使用java的机器人类来模拟按下选项卡并输入任何其他按钮。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM