[英]How to clear text input?
I've been trying to fix this issue for a while now, none of the existing questions help.我已经尝试解决这个问题一段时间了,现有的问题都没有帮助。 I'm using the node implementation of Selenium WebDriver , version ^4.0.0-alpha.1 and testing it on Chrome using the latest ChromeDriver.
我正在使用Selenium WebDriver 的节点实现,版本^4.0.0-alpha.1并使用最新的 ChromeDriver 在 Chrome 上测试它。
I'm trying to clear the text of an <input/>
, whose type="email"
.我正在尝试清除
<input/>
的文本,其type="email"
。 WebDriver has a built in command for this, but when I execute it the input doesn't clear (and there's no error): WebDriver 有一个内置命令,但是当我执行它时,输入没有清除(并且没有错误):
// This populates the text field, no problem
driver.findElement(By.id("model-auth-email")).sendKeys("test@gmail.com");
// This is executed without error, but nothing happens, text field isn't cleared
driver.findElement(By.id("model-auth-email")).clear();
I can't use the CTRL + a & BACKSPACE method since I'm using a Mac and it would be CMD + a .我不能使用CTRL + a & BACKSPACE方法,因为我使用的是 Mac,它会是CMD + a 。 And ChromeDriver has had (still unresolved) issues supporting the native OSX CMD button input for like 6 years, according the various threads I could find.
根据我能找到的各种线程,ChromeDriver 有(仍未解决)支持原生 OSX CMD按钮输入的问题已有 6 年之久。
I could also do something like loop BACKSPACE inputs until it's cleared, though that's pretty hacky.我也可以做一些类似循环BACKSPACE输入的事情,直到它被清除,尽管这很hacky。
Any ideas on why .clear()
is silently not working?关于为什么
.clear()
不工作的任何想法?
我有同样的问题, .clear() 或 CTRL+'a' 对我不起作用,这个工作得很好:
driver.execute_script("arguments[0].value = '';", text_input)
What seems to be working for us is to:似乎对我们有用的是:
select()
method of the <input>
element.<input>
元素的select()
方法。 Here's a clear
function that will do just that:这是一个
clear
功能,可以做到这一点:
async function clear(drv, web_elt) {
await drv.executeScript(elt => elt.select(), web_elt);
await web_elt.sendKeys(Key.BACK_SPACE);
}
Whereby:据此:
drv
is an instance of WebDriver
drv
是WebDriver
一个实例web_elt
is an instance of WebElement
web_elt
是WebElement
一个实例executeScript
is stringified and receives the <input>
element wrapped in web_elt
as a parameter executeScript
的匿名函数被字符串化并接收web_elt
包裹的<input>
元素作为参数Find below a demo and the full code for it.在下面找到一个演示和它的完整代码。
This is running on Node.js 11 with selenium-webdriver
v4.这是在带有
selenium-webdriver
v4 的 Node.js 11 上运行的。
Make sure your chromedriver is in your PATH then run node index.js
确保您的 chromedriver 在您的 PATH 中,然后运行
node index.js
// index.js
const path = require('path');
const wd = require('selenium-webdriver');
const {Key} = wd;
const driver =
(new wd.Builder)
.forBrowser(wd.Browser.CHROME)
.build();
async function clear(drv, web_elt) {
await drv.executeScript(elt => elt.select(), web_elt);
await web_elt.sendKeys(Key.BACK_SPACE);
}
async function run(drv) {
await drv.get('file://' + path.join(__dirname, 'index.html'));
await drv.sleep(1000);
await clear(drv, drv.findElement({css: 'input'}));
await drv.sleep(1000);
await clear(drv, drv.findElement({css: 'textarea'}));
await drv.sleep(1000);
await drv.quit();
}
run(driver);
<!-- index.html -->
<html>
<body>
<p>
<input type="text" value="Hello World!"/>
</p>
<p>
<textarea cols="30" rows="10">Hello World!</textarea>
</p>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.