Java Selenium Web 驱动程序中的 sendKeys()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19268617/
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-12 15:34:42  来源:igfitidea点击:

sendKeys() in Selenium web driver

javaseleniumselenium-webdriver

提问by Niks

I am new to Selenium. I just want to send keys to a username text box and send a tab key both at a time so that text box can check for availability of username.

我是Selenium 的新手。我只想将密钥发送到用户名文本框并同时发送一个制表键,以便文本框可以检查用户名的可用性。

Here is the code:

这是代码:

 driver.findElement(By.xpath("//label[text()='User Name:']/following::div/input")).sendKeys("UserName");
 driver.findElement(By.xpath("//label[text()='User Name:']/following::div/input")).sendKeys(Keys.TAB);

But this one is not working.

但是这个是行不通的。

回答by Paras

I doubt for Keys.TABin sendKeysmethod... if you want to use TAB you need to do something like below:

我怀疑Keys.TABinsendKeys方法......如果你想使用 TAB 你需要做如下的事情:

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

回答by Hemanth

Try using Robotclass in java for pressing TAB key. Use the below code.

尝试使用Robotjava 中的类来按下 TAB 键。使用下面的代码。

driver.findElement(By.xpath("//label[text()='User Name:']/following::div/input")).sendKeys("UserName");

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);

回答by Shik

Try this code:

试试这个代码:

WebElement userName = pathfinderdriver.switchTo().activeElement();
userName.sendKeys(Keys.TAB);

回答by Babu

This is a single line command to send the TAB key;

这是发送 TAB 键的单行命令;

driver.findElement(By.id("Enter_ID")).sendKeys("\t");

回答by Akbar Hussain

List<WebElement>itemNames = wd.findElements(By.cssSelector("a strong")); 
System.out.println("No items in Catalog page: " + itemNames.size());
   for (WebElement itemName:itemNames)
    {  
       System.out.println(itemName.getText());
    }

回答by Piyush Salodkar

Try this, it will surely work:

试试这个,它肯定会起作用:

driver.findElement(By.xpath("//label[text()='User Name:']/following::div/input")).sendKeys("UserName" + Keys.TAB);

回答by NH.

I believe Selenium now uses Key.TABinstead of Keys.TAB.

我相信 Selenium 现在使用Key.TAB而不是Keys.TAB.

回答by varun kumar

Try this one, and then import the package:

试试这个,然后导入包:

import org.openqa.selenium.Keys;

driver.findElement(By.xpath("//*[@id='username']")).sendKeys("username");

driver.findElement(By.xpath("//*[@id='username']")).sendKeys(Keys.TAB);

driver.findElement(By.xpath("//*[@id='Password']")).sendKeys("password");

回答by Shyam

The simplest solution is Go to Build Path > Configure Build Path > Java Compiler and then select the 'Compiler compliance level:' to the latest one from 1.4 (probably you have this).

最简单的解决方案是转到构建路径 > 配置构建路径 > Java 编译器,然后选择“编译器合规性级别:”到 1.4 中的最新级别(可能你有这个)。

回答by Khan Saad

For python selenium,

对于蟒蛇硒,

Importing the library,

导入库,

from selenium.webdriver.common.keys import Keys

Use this code to press any key you want,

使用此代码按您想要的任何键,

Anyelement.send_keys(Keys.RETURN)

You can find all the key names by searching this selenium.webdriver.common.keys.

您可以通过搜索找到所有键名selenium.webdriver.common.keys