java Selenium sendKeys 未发送所有字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37200048/
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
Selenium sendKeys are not sending all characters
提问by Larica B
I'm using Java, Selenium, and Chrome for test automation. Our developers recently upgraded our UI from AngularJS to Angular2 (not sure if that matters). But since then, sendKeys is inputting incomplete characters in to the text field. Here's an example:
我使用 Java、Selenium 和 Chrome 进行测试自动化。我们的开发人员最近将我们的 UI 从 AngularJS 升级到了 Angular2(不确定这是否重要)。但从那以后,sendKeys 将不完整的字符输入到文本字段中。下面是一个例子:
public void enterCustomerDetails()
{
txtFirstName.sendKeys("Joh201605130947AM");
txtSurname.sendKeys("Doe201605130947AM");
txtEmail.sendKeys("[email protected]");
}
I also tried using executeScript. It didn't work. It can enter complete characters but the form thinks the field is null.
我也尝试使用executeScript。它没有用。它可以输入完整的字符,但表单认为该字段为空。
public void forceSendKeys(WebElement element, String text)
{
if (element != null)
((JavascriptExecutor) this.webDriver).executeScript("arguments[0].value=arguments[1]", element, text);
}
public void enterCustomerDetails()
{
forceSendKeys(txtFirstName, "Joh201605130947AM");
forceSendKeys(txtSurname, "Doe201605130947AM");
forceSendKeys(txtEmail, "[email protected]");
}
I also tried using .click() before .sendKeys and adding in sleep time. They didn't work too.
我还尝试在 .sendKeys 之前使用 .click() 并添加睡眠时间。他们也没有工作。
I got an idea to enter the characters 1 by 1 from this post: How to enter characters one by one in to a text field in selenium webdriver?
我从这篇文章中得到了一个一一输入字符的想法:如何在 selenium webdriver 的文本字段中一一输入字符?
It worked but that means I have to rewrite all my codes from sendKeys to the new function:
它有效,但这意味着我必须将所有代码从 sendKeys 重写为新函数:
public void sendChar(WebElement element, String value)
{
element.clear();
for (int i = 0; i < value.length(); i++){
char c = value.charAt(i);
String s = new StringBuilder().append(c).toString();
element.sendKeys(s);
}
}
public void enterCustomerDetails()
{
sendChar(txtFirstName, "Joh201605130947AM");
sendChar(txtSurname, "Doe201605130947AM");
sendChar(txtEmail, "[email protected]");
}
If you guys know a better way, please help! :)
如果你们知道更好的方法,请帮忙!:)
采纳答案by Günter Z?chbauer
I assume this is caused by this Angular2 issue https://github.com/angular/angular/issues/5808
我认为这是由这个 Angular2 问题引起的https://github.com/angular/angular/issues/5808
Angular can't process input events when they arrive too fast.
当输入事件到达太快时,Angular 无法处理它们。
As a workaround you would need to send single characters with a small delay between each.
作为一种解决方法,您需要发送单个字符,每个字符之间的延迟很小。
回答by Arg0n
I stumbled upon this error when doing integration tests with NightwatchJS (which uses selenium).
在使用 NightwatchJS(使用 selenium)进行集成测试时,我偶然发现了这个错误。
So I'm writing this for people coming here in the future.
所以我写这篇文章是为了将来来这里的人。
I wrote this extension commandfor nightwatch:
我为 nightwatch编写了这个扩展命令:
exports.command = function (selector, value, using) {
var self = this;
self.elements(using || 'css selector', selector, function (elems) {
elems.value.forEach(function (element) {
for (var c of value.split('')) {
self.elementIdValue(element.ELEMENT, c);
}
});
});
return this;
};
Which can be used in this way:
可以这样使用:
var username = '[email protected]';
browser.setValueSlow('input[ngcontrol=username]', username); //Works with ng2!
This issue was also discussed on NightwatchJS's github here
这个问题也在 NightwatchJS 的 github 上讨论过
回答by batuarslan
This is due to a bug in Angular apps. Workaround is to put a sleep function.
这是由于 Angular 应用程序中的一个错误。解决方法是放一个睡眠功能。
public void setText(By element, String text) {
sleep(100); // Angular version < 2 apps require this sleep due to a bug
driver.findElement(element).clear();
driver.findElement(element).sendKeys(text);
}
回答by Seunara
I was getting this error too in Java, Selenium. You might also be getting this error too while writing your codes - "sendKeys (CharSequence) from the type Webelement refers to the missing type charSequence"
我在 Java、Selenium 中也遇到了这个错误。您在编写代码时也可能遇到此错误 - “Webelement 类型的 sendKeys (CharSequence) 指的是缺少的 charSequence 类型”
I tried varying the wait time and even typing extra characters before the main characters, they did not work.
我尝试改变等待时间,甚至在主要角色之前输入额外的字符,但它们不起作用。
The simple trick I used was to change the Java Compiler version from JRE 9 to JRE 10.
我使用的简单技巧是将 Java 编译器版本从 JRE 9 更改为 JRE 10。
回答by Julían Hernández Tuyin
i had the same problem, if you see it carefully selenium is changing the characters, some numbers perform a backspace or other symbols, i read it happens when using selenium with vncserver, i changed to firefox.... and it worked.
我遇到了同样的问题,如果你仔细看到 selenium 正在改变字符,一些数字执行退格或其他符号,我读到它在使用 selenium 和 vncserver 时发生,我改为 firefox .... 并且它起作用了。
if that's not your problem, maybe sending the data in parts:
如果这不是您的问题,也许可以分部分发送数据:
input1="Joh201605130947AM"
txtFirstName.sendKeys(input1[0:7])
txtFirstName.sendKeys(input1[8:end])
回答by S. Doe
Using
使用
- Chromium 78.0.3904.70,
- Vaadin Flow Framework 14.1.3,
- Selenium 3.141.59
- and OpenJDK 11.0.5
- 铬 78.0.3904.70,
- Vaadin 流框架 14.1.3,
- 硒 3.141.59
- 和 OpenJDK 11.0.5
the behavior also occurs and is even worse: I see that the character is typed in and suddenly after that it disappears. A workaround is to be persistent and just try it again. And again. Until the character is finally typed in.
这种行为也会发生,甚至更糟:我看到输入的字符突然消失了。解决方法是坚持不懈,然后再试一次。然后再次。直到最终输入字符。
// Type in every single character
for (int i = 0; i < textToType.length(); i++) {
boolean typingCharacterWasSuccessful = false;
// If typing was not successful before, just type again
while (!typingCharacterWasSuccessful) {
// Type in the character
char singleCharacterToType = textToType.charAt(i);
htmlTextfeld.sendKeys(Character.toString(singleCharacterToType));
// Wait a little. Maybe alternatively/additionally wait.until(...)
Thread.sleep(200);
// Check typed in string.
String inputValueAfterTyping = htmlTextfeld.getAttribute("value");
if (inputValueAfterTyping.length() > i + 1) {
// Alternatively: delete everything and start all over
throw new Exception("Input value too long. Maybe character typed in twice!?");
}
// Typing was successful if the value in the input field is as expected (up to now)
typingCharacterWasSuccessful
= inputValueAfterTyping.equals(textToType.substring(0, i + 1));
}
}
回答by Manjunatha.N
try this code.. other way to set values using javascript WebDriver driver = new FirefoxDriver(); JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("document.getElementsByName('body')[0].setAttribute('type', 'text');"); driver.findElement(By.xpath("//input[@name='body']")).clear(); driver.findElement(By.xpath("//input[@name='body']")).sendKeys("Ripon: body text");
试试这个代码.. 使用 javascript WebDriver driver = new FirefoxDriver(); 设置值的其他方法;JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript("document.getElementsByName('body')[0].setAttribute('type', 'text');"); driver.findElement(By.xpath("//input[@name='body']")).clear(); driver.findElement(By.xpath("//input[@name='body']")).sendKeys("Ripon: body text");