[英]How to enter characters one by one in to a text field in selenium webdriver?
How to enter characters one by one in to a text field in selenium webdriver?如何在 selenium webdriver 的文本字段中一个一个地输入字符? I have used the below code but it's not working
我使用了下面的代码,但它不工作
getDriver().findElement(By.id("PhoneNumber")).sendKeys(Keys.chord("9876544322"));
Can anybody suggest how to resolve this?有人可以建议如何解决这个问题吗?
Here is how I am sending character by character using Selenium Webdriver (in Java).这是我如何使用 Selenium Webdriver(在 Java 中)逐个字符发送。 This way in the back-end, I verify at each letter press if the character exists in the input.
这样在后端,我会在每次按下字母时验证输入中是否存在该字符。 Normal
element.sendKeys()
is not working well for me 2 out of 5 times - the last letter is missing, I guess something is buggy with Selenium Webdriver, I don't know.正常的
element.sendKeys()
对我来说有 5 次有 2 次不能正常工作 - 最后一个字母丢失了,我想 Selenium Webdriver 有问题,我不知道。 Try the code below, it works 100% of the time for me.试试下面的代码,它对我来说 100% 的时间都有效。
public void TypeInField(String xpath, String value){
String val = value;
WebElement element = driver.findElement(By.xpath(xpath));
element.clear();
for (int i = 0; i < val.length(); i++){
char c = val.charAt(i);
String s = new StringBuilder().append(c).toString();
element.sendKeys(s);
}
}
As you see, I get the value needed to be typed and in the for
loop, I take each character, convert it to string and send it to textbox.如您所见,我获得了需要输入的值,并在
for
循环中获取每个字符,将其转换为字符串并将其发送到文本框。 Also, I have a search for xpath, you can change that to id, or classname, or whatever you want.另外,我搜索了 xpath,您可以将其更改为 id、类名或任何您想要的。
If you want to make your sendKeys more human like, I've used something like this:如果你想让你的 sendKeys 更人性化,我使用了这样的东西:
private static void sendHumanKeys(WebElement element, String text) {
Random r = new Random();
for(int i = 0; i < text.length(); i++) {
try {
Thread.sleep((int)(r.nextGaussian() * 15 + 100));
} catch(InterruptedException e) {}
String s = new StringBuilder().append(text.charAt(i)).toString();
element.sendKeys(s);
}
}
It sends the keys with a 100ms delay, but only an average of 100ms.它以 100 毫秒的延迟发送密钥,但平均只有 100 毫秒。 It creates a normal distribution with average 100ms and std.
它创建了一个平均 100ms 和 std 的正态分布。 deviation 15ms.
偏差 15ms。
sendKeys()
does enter characters in sequence, but it can at times run quickly enough to be perceived as a copy/paste action. sendKeys()
确实按顺序输入字符,但它有时可以运行得足够快以被视为复制/粘贴操作。 Though, it is in fact intended to simulate a user entering text by typing.尽管如此,它实际上是为了模拟用户通过打字输入文本。 Per the
sendKeys()
JavaDoc:根据
sendKeys()
JavaDoc:
/** Use this method to simulate typing into an element, which may set its value.
/** 使用这个方法来模拟输入一个元素,它可以设置它的值。 */
*/
If you wanted to slow it down, you could make a method that accepts WebElement and String args, convert the String to charsequence[]
, then use a for loop and enter each index of the array in the .sendKeys() followed by a Thread.sleep()
.如果你想减慢它的速度,你可以创建一个接受 WebElement 和 String args 的方法,将 String 转换为
charsequence[]
,然后使用 for 循环并在 .sendKeys() 中输入数组的每个索引,然后是一个Thread.sleep()
。 This seems horribly inefficient, though, as sendKeys()
recognizes Strings as charsequence[]
(String is a charsequence[]
in Java).但是,这似乎效率极低,因为
sendKeys()
将字符串识别为charsequence[]
(String是Java 中的charsequence[]
)。 Adding the Thread.sleep()
will only slow your test needlessly.添加
Thread.sleep()
只会不必要地减慢您的测试速度。
Honestly, sendKeys()
fits your described needs.老实说,
sendKeys()
符合您描述的需求。 It's the best way to simulate a user at a keyboard, it just does it really fast.这是在键盘上模拟用户的最佳方式,它的速度非常快。
.chord() will press all keys simultaneously. .chord() 将同时按下所有键。 Not very well suited for field input.
不太适合现场输入。
The .sendKeys() method will accept a String. .sendKeys() 方法将接受一个字符串。 Simply pass your input as such and have a go with it.
简单地传递您的输入并尝试一下。
driver.findElement(By.id("element")).sendKeys("Field Input Text");
I use this function in my test when I want to type a string letter by letter.当我想一个字母一个字母地输入一个字符串时,我在我的测试中使用这个函数。
public void typeOnLetterByLetter(WebElement webElement, String value, long waitBetweenLetters, ChronoUnit unitTime) {
clear(webElement);
Arrays.asList(value.toCharArray()).forEach(letter -> {
typeOn(webElement, String.valueOf(letter));
pause(waitBetweenLetters, unitTime);
});
}
private void pause(long time, ChronoUnit unitTime) {
try {
Thread.sleep(Duration.of(time, unitTime).toMillis());
} catch (InterruptedException ignore) {
}
}
I created a Python function of the Java Selenium code.我创建了 Java Selenium 代码的 Python 函数。 Find attached below and tweak based on the elements you want to use:
在下面找到附件并根据您要使用的元素进行调整:
def TypeInField(xpath, myValue):
val = myValue
elem = driver.find_element_by_xpath(xpath)
c = ""
for i in range(0, len(val)):
c += val[i]
elem.send_keys(c)
time.sleep(3)
elem.send_keys(Keys.ENTER)
Here is how I did it in C#这是我在 C# 中的做法
public void TypeInFieldCharByChar(IWebElement element,string text)
{
var strBuilder = new StringBuilder();
for (int i = 0; i < text.Length; i++)
{
Thread.Sleep(100);
strBuilder.Append(text[i]);
element.SendKeys(strBuilder.ToString());
strBuilder.Clear();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.