获取元素按属性 [英] Get Elements By Attributes

查看:246
本文介绍了获取元素按属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会很短。

据我所知的Watir库提供了获取HTML元素的两种方法。

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

对于几乎提供的Watir两种方法的每个元素(DIV,按钮,表,里,等):

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...

。第二个是复数的方法,将获取的Watir实例的所有元素

. 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

但据我所知的Watir没有提供一个方法来获得多个元素赋予一定的条件。

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

例如...如果我想闪所有ID链接:my_link_id这将是很容易做这样的事情:

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

使用角度来说,Hpricot这个任务是很容易的......但是如果你的目的不是要分析我找不到的Watir方法我想要做什么。

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...

干杯,胡安!!

推荐答案

你的脚本有几个问题:


  • 您说你要闪所有链接,但你使用 watir_instance.divs 。它应该是 watir_instance.links

  • 您将参数传递给的div 方法: watir_instance.divs(:身份证,my_link_id')。它应该只是 watir_instance.divs

  • 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

您的例子也怪:

我要闪所有链接
  ID:my_link_id

i want to flash all the links with id:my_link_id

据我所知,ID应该是在页面独一无二的。

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

因此​​,这里有不同的例子:

So, here are different examples:

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

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页面上的所有链接有问题在URL(奖金:滚动页面,这样闪烁的链接是可见的):

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

这篇关于获取元素按属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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