简体   繁体   English

Selenium:如何使用javascript清除表单字段中的值?

[英]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对象。 Use window to refer to the window of your application, eg window.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.

相关问题 如何使用Java清除表单上的输入字段-Selenium组合 - How do I clear an input field on a form using Java - Selenium combo 如何使用Selenium验证表单行中的值? - How do I use Selenium to verify the value in a form row? Selenium-如何键入由Javascript生成的文本字段? - Selenium - How do I type into a text field generated by Javascript? 您如何从Selenium访问javascript并提交表单? - How do you access javascript from selenium and submit a form? 如何从 BeautifulSoup 抓取中提取 onclick 事件 Javascript 文本,然后与 ZC49DFB55F06F8406E38ZCCA_script 一起使用? - How do I extract onclick event Javascript text from BeautifulSoup scrape to then use with Selenium execute_script? 如何使用硒测试javascript图像上传? - how do i use selenium to test javascript image upload? 如何使用selenium来更改输入值[type ='range'] - How do I use selenium to change the value of a input[type='range'] 如何清除在文本字段中输入的先前值并将新值传递到Selenium Webdriver中的同一字段 - How to clear the previous value entered in text field and pass new value to same field in Selenium webdriver 如何从Selenium获取JavaScript值? - How do I get JavaScript Values from Selenium? 如何为硒selenium.useXpathLibrary(“ javascript-xpath”);设置此值? - How do I set this value for selenium selenium.useXpathLibrary(“javascript-xpath”);?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM