如何使用 Watir 将值插入到 div 下的文本字段中
我使用 set
在 div 下的文本字段中插入一个值。
以下是我使用过但没有成功的方法。这是我能够识别该元素的唯一方法。当我尝试按名称识别文本字段时,无法识别。
@browser.div(:evaluation, :id => "evaluation_form_attributes").text_field(:id => "evaluation_form_name")
@browser.set('Teacher Evaluation Form')
显示以下错误:
undefined method `set' for #<Watir::IE:0x4dd9348>
这是 HTML:
div id="evaluation_form_attributes"
评估名称:
input id="evaluation_form_name" type="text" size="50" name="evaluation_form[name]" maxlength="30"
I used set
to insert a value in a text field under a div.
Below is the approach that I've used without success. This is the only way I was able to identify the element. When I tried to identify text field by name was not recognized.
@browser.div(:evaluation, :id => "evaluation_form_attributes").text_field(:id => "evaluation_form_name")
@browser.set('Teacher Evaluation Form')
The following error was displayed:
undefined method `set' for #<Watir::IE:0x4dd9348>
This is the HTML:
div id="evaluation_form_attributes"
Evaluation name:
input id="evaluation_form_name" type="text" size="50" name="evaluation_form[name]" maxlength="30"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
Try this:
也许涉及 iframe 吗?如果您知道该东西在那里,并且您确定 ID,但 watir 无法找到它,那么通常是因为页面的该部分位于框架内。
要使用框架内的元素,您必须指定框架,然后指定元素
browser.frame(:how, What).element(:how, What).method 等。
请参阅 http://wiki.openqa.org/display/WTR/Frames 了解更多信息
Is there an iframe involved perhaps? if you know the thing is there, and you are sure of the ID, and watir cannot locate it, then usually it's because that part of the page is inside of a frame.
To work with elements that are inside of a frame, you must specify the frame, then the element
browser.frame(:how, what).element(:how, what).method etc.
see http://wiki.openqa.org/display/WTR/Frames for more info
要为文本字段设置值,您只需找到它并设置它即可。您已经知道设置部分 (
set
),因此您需要将查找文本字段的内容串在一起。如果 Zejiko 的答案由于“无法定位元素”而不起作用,那么这不是设置失败,而是找到文本字段。使用 Firebug(在 Firefox 中)或某种 DOM 工具包(工具>开发人员工具或 IE8 中的 F12)来查找文本字段并查看它具有哪些属性。
确保 ID 确实是
“evaluation_form_name”
。这区分大小写并且对前导/尾随空格敏感。您可以尝试以下操作来扩大搜索范围:这使用正则表达式来标识 id。您可以通过许多内容进行搜索,而不仅仅是 :id,并且您可以使用多个内容。例如:
查找 id 包含“eval”的第二个文本字段。
您可以在此处找到可用于识别文本字段的内容:https ://github.com/watir/watir/wiki/HTML-Elements-Supported-by-Watir
To set a value for a text field you simply need to find it and set it. The setting part you already know (
set
), so you need to string together something that finds the text field.if Zejiko's answer doesn't work because of "unable to locate element" then it's not the setting that's failing, it's finding the text field. Use Firebug (in Firefox) or some kind of DOM toolkit (Tools>Developer Tools or F12 in IE8) to find the text field and see what kind of attributes it has.
Ensure the id is really
"evaluation_form_name"
. This is case sensitive and sensitive to leading/trailing spaces. You could try the following to make your search broader:This uses a regular expression to identify the id. You can search by many things, not just :id, and you can use more than one. For example:
finds the second text field whose id contains "eval".
What you can use to identify the text field can be found here: https://github.com/watir/watir/wiki/HTML-Elements-Supported-by-Watir