简体   繁体   English

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

[英]How to press “TAB” then “ENTER” key in Selenium WebDriver using Java?

I am doing automation testing using java with Selenium WebDriver. 我正在使用带有Selenium WebDriver的java进行自动化测试。 I want to click on tabs. 我想点击标签。 I would like to check tab functionality. 我想检查标签功能。

I can use Tab key to get the button as below: 我可以使用Tab键获取如下按钮:

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. 基本上我需要按Tab键,然后按Enter键单击按钮。

I learning selenium. 我学习硒。 Please help me. 请帮我。 Thanks in advance. 提前致谢。

Please see the solution that works with my example form 请参阅适用于我的示例表单的解决方案

FormTab.html: 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: 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();

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的机器人类来模拟按下选项卡并输入任何其他按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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