如何在 Watir 中查找文本框
我正在尝试使用 watir-webdriver 访问网站,但我似乎无法在 watir 中找到我可以在 Firefox+Firebug 中看到的文本框。
我的代码是
require 'rubygems'
require 'irb/completion'
require 'watir-webdriver'
browser = Watir::Browser.new(:firefox)
browser.goto('http://emersonecologics.com/')
browser.text_field(:name, "txtEmail").set("[email protected]")
我收到错误:
Watir::Exception::UnknownObjectException: unable to locate element, using {:type=>"(any text type)", :name=>"txtEmail", :tag_name=>"input or textarea"}
但是,我知道有一个名为 txtEmail 的文本框,因为 Firebug 向我显示了
<input id="txtEmail" class="textbox" type="text" tabindex="1" name="txtEmail">
当然,这个文本框位于树的深处。因此,我认为应该在 DOM 中导航到它,因此尝试访问名为“all”的 div。
如果我这样做
>>browser.divs[1].id
=> "all"
>> browser.divs[1].tag_name
=> "div"
但是当我尝试按如下方式获取它的句柄时,似乎我无法找到它。
>>browser.div(:id, "all")
=> #<Watir::Div:0x101a8fd70 located=false selector={:tag_name=>"div", :id=>"all"}>
谁能帮助我如何选择页面中的对象?
I am trying to access a site with watir-webdriver, but I can't seem to find a textbox in watir that I can see in Firefox+Firebug.
My code is
require 'rubygems'
require 'irb/completion'
require 'watir-webdriver'
browser = Watir::Browser.new(:firefox)
browser.goto('http://emersonecologics.com/')
browser.text_field(:name, "txtEmail").set("[email protected]")
I get the error:
Watir::Exception::UnknownObjectException: unable to locate element, using {:type=>"(any text type)", :name=>"txtEmail", :tag_name=>"input or textarea"}
However, I know there is a textbox named txtEmail because Firebug shows me
<input id="txtEmail" class="textbox" type="text" tabindex="1" name="txtEmail">
Of course, it is this textbox is deep inside the tree. So, thinking that I should navigate to it in the DOM, I tried to access the div called 'all'.
If I do
>>browser.divs[1].id
=> "all"
>> browser.divs[1].tag_name
=> "div"
But when I try to get a handle to it as following, it seems I cant locate it.
>>browser.div(:id, "all")
=> #<Watir::Div:0x101a8fd70 located=false selector={:tag_name=>"div", :id=>"all"}>
Can anyone help me how to select objects in the page?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对我来说,它似乎在一个框架中,也是
为了避免索引混淆(特别是考虑到 Watir 的各种风格是基于 0 或基于 1 的差异),我建议通过名称来识别它。看看这个会不会闪。
请参阅 有关框架的 Watir Wiki 页面,了解有关处理框架内内容的更多
信息
注意(与网站相关,以及框架的使用):您可能还向网站开发人员提到,某些浏览器(例如 Chrome)注意到框架 (https) 的协议与外部容器 (http) 不匹配 Chrome 的协议不一致开发人员工具发出以下投诉
Looks like it's in a frame to me also
to avoid confusion over indexes (especially given variences in how various flavors of Watir are either 0 based or 1 based) I'd suggest identifying it by name. See if this will flash it.
See the Watir Wiki page on frames for more info on dealing with stuff inside a frame
P.S.
Note (related to the site, and it's use of frames): You might also mention to the site developers that some browsers such as chrome are noting an inconsistency of the protocol for the frame (https) not matching the outer container (http) Chrome's developer tools is issuing the following complaint
您尝试访问的元素似乎位于框架内。我可以使用以下命令设置电子邮件:
browser.frame(:index, 1).text_field(:name, "txtEmail").set("[电子邮件受保护]")
欲了解更多信息:http://wiki.openqa.org/display/WTR/Frames
It appears the element you are trying to access is inside a frame. I was able to set the email with:
browser.frame(:index, 1).text_field(:name, "txtEmail").set("[email protected]")
For more info: http://wiki.openqa.org/display/WTR/Frames