如何在 Watir 中查找文本框

发布于 2024-11-14 10:52:14 字数 1230 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑凤梨 2024-11-21 10:52:14

对我来说,它似乎在一个框架中,也是

为了避免索引混淆(特别是考虑到 Watir 的各种风格是基于 0 或基于 1 的差异),我建议通过名称来识别它。看看这个会不会闪。

browser.frame(:id, "ctrlLoginSSL_ifLogin").text_field(:id, "txtEmail").flash

请参阅 有关框架的 Watir Wiki 页面,了解有关处理框架内内容的更多

信息

注意(与网站相关,以及框架的使用):您可能还向网站开发人员提到,某些浏览器(例如 Chrome)注意到框架 (https) 的协议与外部容器 (http) 不匹配 Chrome 的协议不一致开发人员工具发出以下投诉

不安全的 JavaScript 尝试访问
带有 URL 的框架
https://www.emersonecologics.com/User/LoginFrame.aspx? redir=/default.aspx
来自带有 URL 的框架
http://emersonecologics.com/。域名,
协议和端口必须匹配。

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.

browser.frame(:id, "ctrlLoginSSL_ifLogin").text_field(:id, "txtEmail").flash

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

Unsafe JavaScript attempt to access
frame with URL
https://www.emersonecologics.com/User/LoginFrame.aspx?redir=/default.aspx
from frame with URL
http://emersonecologics.com/. Domains,
protocols and ports must match.

如何视而不见 2024-11-21 10:52:14

您尝试访问的元素似乎位于框架内。我可以使用以下命令设置电子邮件:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文