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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29768448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:34:10  来源:igfitidea点击:

How to press "TAB" then "ENTER" key in Selenium WebDriver using Java?

javaseleniumselenium-webdrivertabs

提问by Deepak gupta

I am doing automation testing using java with Selenium WebDriver. I want to click on tabs. I would like to check tab functionality.

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

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 image

我有一个包含多个字段的表单,我想在按下 Tab 键时跟踪我的控件是否成功移动到下一个字段。另外我想检查我的控件下面是我的表单图片

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.

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

回答by Eugene

Please see the solution that works with my example form

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

FormTab.html:

表格Tab.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();

回答by anuja jain

Actions builder = new Actions(driver);
        Action enter= builder
                .keyDown(Keys.TAB)
                .build();
enter.perform();

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

回答by E.E

You can try to use robot class of java to simulate pressing tabs and enters ot any other buttons , as much as you want, when you are on the page.

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

回答by SaiPawan

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();