通过属性获取元素

发布于 2024-08-05 03:19:38 字数 773 浏览 0 评论 0原文

我会很短。

据我所知,watir 库提供了两种获取 html 元素的方法。

几乎对于每个元素(div、button、table、li 等)watir 都提供了两种方法:

.一种是“单一”方法,仅获取一个特定元素。例如:

watir_instance.div(:id,'my_div_id')
watir_instance.link(:href,'my_link_href')
watir_instance.button(:class =>'my_button_class', :index => 4)

这些方法将仅检索单个元素。没关系...

。第二种是“复数”方法,它将检索 watir 实例的所有元素

watir_instance.divs
watir_instance.links
watir_instance.buttons

,但据我所知,watir 没有提供一种在给定条件的情况下获取多个元素的方法。

例如...如果我想刷新所有带有 id:my_link_id 的链接,那么做这样的事情会很容易:

watir_instance.divs(:id, 'my_link_id').each do |link|
  link.flash
end

使用 hpricot 这个任务非常简单...但是如果你的目标不是解析我就不能找到一个满足我要求的 Watir 方法。

希望你能理解我...

干杯,胡安!

i will be short.

As far as i know watir library provides two methods for getting html elements.

Almost for each element (div, button, table, li, etc) watir provides two methods:

. One is the 'singular' method which gets only one specific element. For example:

watir_instance.div(:id,'my_div_id')
watir_instance.link(:href,'my_link_href')
watir_instance.button(:class =>'my_button_class', :index => 4)

These methods will only retrieve A SINGLE ELEMENT. Thats ok...

. The second is the 'plural' method that will retrieve ALL the elements of the watir instance

watir_instance.divs
watir_instance.links
watir_instance.buttons

But as far as i know watir doesn't provide a method to get more than one element giving certain conditions.

For example... If i want to flash all the links with id:my_link_id it would be very easy to do something like this:

watir_instance.divs(:id, 'my_link_id').each do |link|
  link.flash
end

With hpricot this task is very easy... but if your aim is not to parse i couldn't find a Watir Method that does what i want.

Hope you can understand me...

Cheers, Juan!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

娇纵 2024-08-12 03:19:38

Juan,

您的脚本有几个问题:

  • 您说您想要刷新所有链接,但随后您使用了 watir_instance.divs。 将参数传递给 divs 方法的应该是 watir_instance.links
  • watir_instance.divs(:id, 'my_link_id')。它应该只是 watir_instance.divs

你的例子也很奇怪:

我想闪烁所有链接
id:my_link_id

据我所知,id 在页面上应该是唯一的。

因此,这里有不同的示例:

1) 闪现此页面上的所有链接:

require "watir"
b = Watir::IE.start "http://stackoverflow.com/questions/1434697"
b.links.each do |link|
  link.flash
end

2) 闪现此页面上 URL 中有问题的所有链接(奖励:滚动页面,以便闪现的链接可见) ):

require "watir"
b = Watir::IE.start "http://stackoverflow.com/questions/1434697"
b.links.each do |link|
  if link.href =~ /questions/
    link.document.scrollintoview
    link.flash
  end
end

Juan,

your script has several problems:

  • You say you want to flash all links, but then you use watir_instance.divs. It should be watir_instance.links
  • you pass arguments to divs method: watir_instance.divs(:id, 'my_link_id'). It should be just watir_instance.divs

Your example is also strange:

i want to flash all the links with
id:my_link_id

As far as I know, id should be unique at the page.

So, here are different examples:

1) Flash all links on this page:

require "watir"
b = Watir::IE.start "http://stackoverflow.com/questions/1434697"
b.links.each do |link|
  link.flash
end

2) Flash all links on this page that have questions in URL (bonus: scroll the page so the link that is flashed is visible):

require "watir"
b = Watir::IE.start "http://stackoverflow.com/questions/1434697"
b.links.each do |link|
  if link.href =~ /questions/
    link.document.scrollintoview
    link.flash
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文