Selenium WebDriver:我想覆盖字段中的值,而不是使用 Java 通过 sendKeys 附加到它
在 WebDriver 中,如果我使用 sendKeys,它会将我的字符串附加到字段中已存在的值中。我无法使用clear()方法清除它,因为我第二次这样做时,网页会抛出一个错误,说它必须在10到100之间。所以我无法清除它,否则会在之前抛出错误我可以使用 sendKeys 输入新值,如果我 sendKeys 它只是将其附加到已有的值中。
WebDriver 中是否有任何内容可以让您覆盖字段中的值?
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.
Is there anything in WebDriver that lets you overwrite the value in the field?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您还可以在发送密钥之前清除该字段。
You can also clear the field before sending it keys.
我认为您可以尝试首先选择字段中的所有文本,然后发送新序列:
I think you can try to firstly select all the text in the field and then send the new sequence:
好吧,那是前几天的事了……
在我目前的情况下,ZloiAdun 的答案对我不起作用,但让我非常接近我的解决方案......
而不是:
以下代码让我很高兴:
所以我希望这对某人有帮助!
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...
Instead of:
the following code makes me happy:
So I hope that helps somebody!
如果它对任何人有帮助,ZloiAdun 的答案的 C# 等效项是:
In case it helps anyone, the C# equivalent of ZloiAdun's answer is:
这对我有用。
This worked for me.
使用这个,它是值得信赖的解决方案,并且适用于所有浏览器:
如果输入有 type="file" - 不要在 IE 中清除它。它将尝试通过空路径查找文件并抛出错误。
您可以在我的博客上找到更多详细信息
Use this one, it is trusted solution and works well for all browsers:
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.
More details you could find on my blog
使用大多数提到的方法时遇到问题,因为文本字段不接受键盘输入,并且鼠标解决方案似乎不完整。
这可以模拟在字段中单击,选择内容并将其替换为新内容。
Had issues using most of the mentioned methods since textfield had not accepted keyboard input, and the mouse solution seem not complete.
This worked for to simulate a click in the field, selecting the content and replacing it with new.
使用以下内容:
Use the following:
原来的问题说不能使用clear()。这不适用于那种情况。我在这里添加我的工作示例,因为这篇 SO 帖子是在输入值之前清除输入的第一个 Google 结果之一。
对于没有额外限制的输入,我使用 NodeJS 为 Selenium 提供了一个与浏览器无关的方法。此代码片段是我在测试脚本中使用
var test = require( 'common' );
导入的公共库的一部分。它用于标准节点 module.exports 定义。找到该元素,单击它,清除它,然后发送密钥。
此页面有完整的代码示例以及可能有帮助的文章。
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.
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.Find the element, click it, clear it, then send the keys.
This page has a complete code sample and article that may help.
这是很容易做到的事情,并且对我有用:
55 = 分配的值
This is something easy to do and it worked for me:
55 = value assigned
当我必须处理嵌入 JavaScript 的 HTML 页面时,这解决了我的问题
This solved my problem when I had to deal with HTML page with embedded JavaScript