Selenium WebDriver:我想覆盖字段中的值,而不是使用 Java 用 sendKeys 附加到它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3249583/
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 WebDriver: I want to overwrite value in field instead of appending to it with sendKeys using Java
提问by True_Blue
In WebDriver, if I use sendKeys it will append my string to the value that already exists in the field. I can't clear it by using clear() method because the second I do that, the webpage will throw an error saying that it has to be between 10 and 100. So I can't clear it or an error will be thrown before I can put in the new value using sendKeys, and if I sendKeys it just appends it to the value already there.
在 WebDriver 中,如果我使用 sendKeys,它会将我的字符串附加到字段中已存在的值。我无法使用 clear() 方法清除它,因为我第二次这样做时,网页会抛出一个错误,说它必须在 10 到 100 之间。所以我无法清除它,否则之前会抛出错误我可以使用 sendKeys 输入新值,如果我使用 sendKeys,它只是将它附加到已经存在的值上。
Is there anything in WebDriver that lets you overwrite the value in the field?
WebDriver 中是否有任何内容可以让您覆盖该字段中的值?
采纳答案by Sergii Pozharov
I think you can try to firstly select all the text in the field and then send the new sequence:
我认为您可以尝试先选择字段中的所有文本,然后发送新序列:
from selenium.webdriver.common.keys import Keys
element.sendKeys(Keys.chord(Keys.CONTROL, "a"), "55");
回答by Tim Banks
You can also clear the field before sending it keys.
您还可以在发送密钥之前清除该字段。
element.clear()
element.sendKeys("Some text here")
回答by Gadget
Use this one, it is trusted solution and works well for all browsers:
使用这个,它是值得信赖的解决方案,适用于所有浏览器:
protected void clearInput(WebElement webElement) {
// isIE() - just checks is it IE or not - use your own implementation
if (isIE() && "file".equals(webElement.getAttribute("type"))) {
// workaround
// if IE and input's type is file - do not try to clear it.
// If you send:
// - empty string - it will find file by empty path
// - backspace char - it will process like a non-visible char
// In both cases it will throw a bug.
//
// Just replace it with new value when it is need to.
} else {
// if you have no StringUtils in project, check value still empty yet
while (!StringUtils.isEmpty(webElement.getAttribute("value"))) {
// "\u0008" - is backspace char
webElement.sendKeys("\u0008");
}
}
}
If input has type="file" - do not clear it for IE. It will try to find file by empty path and will throw a bug.
如果输入有 type="file" - 不要为 IE 清除它。它将尝试通过空路径查找文件并抛出错误。
More details you could find on my blog
你可以在我的博客上找到更多细节
回答by ChangeRequest
Okay, it is a few days ago... In my current case, the answer from ZloiAdun does not work for me, but brings me very close to my solution...
好的,这是几天前......在我目前的情况下,ZloiAdun的答案对我不起作用,但让我非常接近我的解决方案......
Instead of:
代替:
element.sendKeys(Keys.chord(Keys.CONTROL, "a"), "55");
the following code makes me happy:
下面的代码让我很高兴:
element.sendKeys(Keys.HOME, Keys.chord(Keys.SHIFT, Keys.END), "55");
So I hope that helps somebody!
所以我希望对某人有所帮助!
回答by Noah Heldman
In case it helps anyone, the C# equivalent of ZloiAdun's answer is:
如果它对任何人有帮助,ZloiAdun 的 C# 等价物的答案是:
element.SendKeys(Keys.Control + "a");
element.SendKeys("55");
回答by Prasanth RJ
This solved my problem when I had to deal with HTML page with embedded JavaScript
当我不得不处理带有嵌入式 JavaScript 的 HTML 页面时,这解决了我的问题
WebElement empSalary = driver.findElement(By.xpath(PayComponentAmount));
Actions mouse2 = new Actions(driver);
mouse2.clickAndHold(empSalary).sendKeys(Keys.chord(Keys.CONTROL, "a"), "1234").build().perform();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].onchange()", empSalary);
回答by Guy Engel
This worked for me.
这对我有用。
mElement.sendKeys(Keys.HOME,Keys.chord(Keys.SHIFT,Keys.END),MY_VALUE);
回答by Boris Imenitov
Had issues using most of the mentioned methods since textfield had not accepted keyboard input, and the mouse solution seem not complete.
由于 textfield 不接受键盘输入,因此在使用大多数提到的方法时遇到问题,并且鼠标解决方案似乎不完整。
This worked for to simulate a click in the field, selecting the content and replacing it with new.
这适用于模拟字段中的点击,选择内容并将其替换为新的。
Actions actionList = new Actions(driver);
actionList.clickAndHold(WebElement).sendKeys(newTextFieldString).
release().build().perform();
回答by Lance Cleveland
The original question says clear() cannot be used. This does not apply to that situation. I'm adding my working example here as this SO post was one of the first Google results for clearing an input before entering a value.
原始问题说不能使用 clear() 。这不适用于那种情况。我在这里添加我的工作示例,因为这个 SO 帖子是在输入值之前清除输入的第一个 Google 结果之一。
For input where here is no additional restriction I'm including a browser agnostic method for Selenium using NodeJS. This snippet is part of a common library I import with var test = require( 'common' );
in my test scripts. It is for a standard node module.exports definition.
对于这里没有额外限制的输入,我使用 NodeJS 为 Selenium 包含了一个浏览器不可知的方法。此代码段是我var test = require( 'common' );
在测试脚本中导入的公共库的一部分。它用于标准节点 module.exports 定义。
when_id_exists_type : function( id, value ) {
driver.wait( webdriver.until.elementLocated( webdriver.By.id( id ) ) , 3000 )
.then( function() {
var el = driver.findElement( webdriver.By.id( id ) );
el.click();
el.clear();
el.sendKeys( value );
});
},
Find the element, click it, clear it, then send the keys.
找到元素,单击它,清除它,然后发送密钥。
This page has a complete code sample and articlethat may help.
这个页面有一个完整的代码示例和文章,可能会有所帮助。
回答by Akhil
Use the following:
使用以下内容:
driver.findElement(By.id("id")).sendKeys(Keys.chord(Keys.CONTROL, "a", Keys.DELETE), "Your Value");