我正在使用 Java 和 Selenium WebDriver 进行自动化测试。我想点击选项卡。我想检查选项卡功能。
我可以使用 Tab 键来获取按钮,如下所示:
WebElement webElement = driver.findElementByXPath("");
webElement.sendKeys(Keys.TAB);
webElement.sendKeys(Keys.ENTER);
我有一个包含多个字段的表单,我想在按下 Tab 键时跟踪我的控件是否成功移动到下一个字段。我还想检查我的控件在哪个下方是我的表格
但是我要如何一个一个地点击tab呢。 基本上我需要实现按Tab键然后按Enter键点击按钮。
我在学习 Selenium 。 请帮我。提前致谢。
最佳答案
请查看适用于我的示例表单的解决方案
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代码:
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();
关于java - 如何使用 Java 在 Selenium WebDriver 中按 "TAB"然后按 "ENTER"键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29768448/