I am trying to search/select a link in a page that is underlined, while others are not. The source is something like this:

<a href="someurl1">
<b>
<u>Some ulined text</u>
</b>
<u></u>
</a>
<br>
<a href="someurl2">Other link text</a>
<br>
<a href="someurl3">Another Link text</a>
<br>

I tried something like

link = browser.link(:u?, true)
link.exists?

I get the following errors

TypeError: expected one of [String, Regexp], got true:TrueClass
    from /usr/lib/ruby/gems/1.8/gems/watir-webdriver-0.2.4/lib/watir-webdriver/locators/element_locator.rb:152:in `check_type'
    from /usr/lib/ruby/gems/1.8/gems/watir-webdriver-0.2.4/lib/watir-webdriver/locators/element_locator.rb:189:in `normalized_selector'
    from /usr/lib/ruby/gems/1.8/gems/watir-webdriver-0.2.4/lib/watir-webdriver/locators/element_locator.rb:188:in `each'
    from /usr/lib/ruby/gems/1.8/gems/watir-webdriver-0.2.4/lib/watir-webdriver/locators/element_locator.rb:188:in `normalized_selector'
    from /usr/lib/ruby/gems/1.8/gems/watir-webdriver-0.2.4/lib/watir-webdriver/locators/element_locator.rb:76:in `find_first_by_multiple'
    from /usr/lib/ruby/gems/1.8/gems/watir-webdriver-0.2.4/lib/watir-webdriver/locators/element_locator.rb:33:in `locate'
    from /usr/lib/ruby/gems/1.8/gems/watir-webdriver-0.2.4/lib/watir-webdriver/elements/element.rb:260:in `locate'
    from /usr/lib/ruby/gems/1.8/gems/watir-webdriver-0.2.4/lib/watir-webdriver/elements/element.rb:247:in `assert_exists'
    from /usr/lib/ruby/gems/1.8/gems/watir-webdriver-0.2.4/lib/watir-webdriver/elements/element.rb:31:in `exist?'

Edit: So I am actually using this for screen scraping rather than testing. That may explain the reasons why watir does not support this directly since CSS and other better practices make sense for testing and when you the HTML development and testing go hand in hand. Hoserver from a scraping perspective, the text formatting is what the user sees, and searching underlined, bold links etc. make sense for scraping.

有帮助吗?

解决方案

I've never seen that kind of attribute used in a test case before. I also haven't seen any code support for it. You may have to roll your own. Here is an example borrowed from Zeljko

def hasUnderlined(browser)
  s = false
  browser.links.each do |l| 
    if l.html.downcase.match /\<u\>*\<\/u\>/
      s = true
    end
  end
end

def getUnderlined(browser)
  browser.links.each do |l| 
    if l.html.downcase.match /\<u\>*\<\/u\>/
      return l
    end
  end
end

其他提示

I don't think what you want is possible directly because the underline is not an attribute of the link tag, but a formatting tag that apples to just the text in the link.

However, in modern web pages, formatting is often controlled by a combination of CSS and attributes such as class names, which ARE something you could specify when identifying a link. So IMHO your best bet here might be to talk a little with your developers about how they are coding the site and see if they are perhaps open to increasing the testability of their code by using slightly more modern techniques for controlling what links are underlined, such as say using CSS and basing the underlining on a class name. (There's a lot of other good reasons to use CSS for controlling formatting instead of embedding it directly in the HTML, but unless your guys are fresh off the html-banana-boat so to speak, they should not need to be taught why using CSS is a good thing)

That would let you search for a link according to the class attribute that was being used to cause CSS to underline the text

If your developers are not open to such an approach to make their code more testable, then I think your only option is going to be to create your own ruby code for this and modify your copy of water (see @Dave's answer), and then be prepared to maintain that custom patch any time you update watir etc.

So, the only thing you know about a link is that it is underlined?

I thought this would do it (using the latest watir-webdriver gem):

browser.link.u.click

But I got this:

NoMethodError: undefined method `u' for #<Watir::Anchor:0x00000100cbb3c0>

Jari (watir-webdriver developer) said he thinks u tag is not in HTML spec.

By the way, this works:

browser.link.b.click

Jari suggested trying xpath, but I thought css selectors would be nicer to read. Here it is:

browser.element(:css => "a b u").click
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top