如何使用Java在Selenium WebDriver中按"TAB"然后按"ENTER"键?

Dee*_*pta 5 java selenium tabs selenium-webdriver

我正在使用带有Selenium WebDriver的java进行自动化测试.我想点击标签.我想检查标签功能.

我可以使用Tab键获取如下按钮:

WebElement webElement = driver.findElementByXPath("");
webElement.sendKeys(Keys.TAB);
webElement.sendKeys(Keys.ENTER);
Run Code Online (Sandbox Code Playgroud)

我有一个包含多个字段的表单,我想跟踪按键标签键是我的控件成功移动到下一个字段.另外,我想检查下面的控件是我的表格图片

但是如何逐个单击选项卡.基本上我需要按Tab键,然后按Enter键单击按钮.

我学习硒.请帮我.提前致谢.

Eug*_*ene 3

请参阅适用于我的示例表单的解决方案

表单选项卡.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>
Run Code Online (Sandbox Code Playgroud)

Java代码:

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();
Run Code Online (Sandbox Code Playgroud)