[英]Selenium: How do I use javascript to clear a value from a form field?
I'm using the selenium IDE and the Selenium-Fitnesse Bridge fixture and I'm trying to test that when I clear a default value out of a form field, my form displays an error message. 我正在使用selenium IDE和Selenium-Fitnesse Bridge夹具,我正在尝试测试当我从表单字段中清除默认值时,我的表单显示错误消息。
So when I record with the Selenium IDE, what it does is the equivalent of telling Selenium to type nothing. 因此,当我使用Selenium IDE进行录制时,它的作用相当于告诉Selenium什么都不输入。
| type | text_field | |
The problem with this is that the Fitnesse fixture I'm using expects that second argument to not be null. 这个问题是我正在使用的Fitnesse夹具期望第二个参数不为空。
Is there a way in Selenium to "clear a value" rather than "typing nothing"? Selenium有没有办法“清除价值”而不是“不打字”?
You can do it via javascript as such: 你可以通过javascript这样做:
| verifyEval | javascript{this.browserbot.getCurrentWindow().document.getElementById('CONTROL_ID').value = ''} ||
Effectively the verifyEval statement allows you to execute any piece of javascript that you'd like. 有效地,verifyEval语句允许您执行任何您想要的javascript。 Makes some difficult problems to accomplish with Selenium much simpler. 使Selenium更难实现一些难题。
I used this tutorial (today believe it or not) to figure things out. 我用这个教程 (今天信不信由你)来解决问题。
I used this to get it to work. 我用它来使它工作。 reg_start_date
is the id of my input field. reg_start_date
是我输入字段的id。
| storeEval | window.document.getElementById('reg_start_date').value = '' |
From the selenium reference: 从硒参考:
Note that, by default, the snippet will run in the context of the "selenium" object itself, so
this
will refer to the Selenium object. 请注意,默认情况下,代码段将在“selenium”对象本身的上下文中运行,因此this
将引用Selenium对象。 Usewindow
to refer to the window of your application, egwindow.document.getElementById('foo')
使用window
来引用应用程序的窗口,例如window.document.getElementById('foo')
Also, avoid wrapping the javascript code with javascript{}, it will not work. 此外,避免使用javascript {}包装javascript代码,它将无法正常工作。
We had this issue at the beginning as well. 我们一开始也有这个问题。 Try: | 尝试:| type | 类型| text_field | text_field | blank | 空白|
Look for more info on the Fitnesse documentation for the use of blank and null. 有关使用空白和null的Fitnesse文档,请查找更多信息。
:) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.