Selenium:如何使用javascript清除表单字段中的值? [英] Selenium: How do I use javascript to clear a value from a form field?

查看:217
本文介绍了Selenium:如何使用javascript清除表单字段中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium IDE和Selenium-Fitnesse Bridge固定装置,并且试图测试当我从表单字段中清除默认值时,我的表单显示一条错误消息.

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不键入任何内容.

So when I record with the Selenium IDE, what it does is the equivalent of telling Selenium to type nothing.

| type | text_field |  |

问题是我正在使用的Fitnesse灯具期望第二个参数不为空.

The problem with this is that the Fitnesse fixture I'm using expects that second argument to not be null.

Selenium中是否有一种清除值"而不是不键入任何内容"的方法?

Is there a way in Selenium to "clear a value" rather than "typing nothing"?

推荐答案

我用它来使其正常工作. reg_start_date是我的输入字段的ID.

I used this to get it to work. reg_start_date is the id of my input field.

| storeEval | window.document.getElementById('reg_start_date').value = '' |

根据硒参考:

请注意,默认情况下,该代码段将在"selenium"对象本身的上下文中运行,因此this将引用Selenium对象.使用window引用您的应用程序窗口,例如window.document.getElementById('foo')

Note that, by default, the snippet will run in the context of the "selenium" object itself, so this will refer to the Selenium object. Use window to refer to the window of your application, e.g. window.document.getElementById('foo')

此外,请避免使用javascript {}包装javascript代码,否则将无法正常工作.

Also, avoid wrapping the javascript code with javascript{}, it will not work.

这篇关于Selenium:如何使用javascript清除表单字段中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆