在 watir 中搜索带下划线的链接
我正在尝试搜索/选择页面中带下划线的链接,而其他页面则没有。来源是这样的:
<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>
我尝试了类似的方法
link = browser.link(:u?, true)
link.exists?
,但出现以下错误
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?'
编辑: 所以我实际上用它来进行屏幕抓取而不是测试。这也许可以解释为什么 watir 不直接支持这一点,因为 CSS 和其他更好的实践对于测试有意义,并且当 HTML 开发和测试齐头并进时。从抓取的角度来看,文本格式是用户看到的,搜索带下划线、粗体的链接等对于抓取来说是有意义的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我以前从未在测试用例中见过这种属性。我也没有看到任何代码支持它。你可能必须自己动手。这是借自 Zeljko 的示例
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
我认为您想要的东西不可能直接实现,因为下划线不是链接标记的属性,而是仅适用于链接中文本的格式标记。
然而,在现代网页中,格式通常由 CSS 和类名等属性的组合来控制,这些属性是您在识别链接时可以指定的内容。因此,恕我直言,您最好的选择可能是与您的开发人员讨论一下他们如何对网站进行编码,并看看他们是否愿意通过使用稍微更现代的技术来控制下划线的链接来提高代码的可测试性,例如就像使用 CSS 并根据类名添加下划线一样。 (还有很多其他充分的理由使用 CSS 来控制格式,而不是直接将其嵌入到 HTML 中,但是除非你们是刚接触 html-banana-boat,否则他们不需要被教导为什么使用 CSS是一件好事)
这将允许您根据用于导致 CSS 在文本下划线的类属性搜索链接
如果您的开发人员不接受这种方法来使他们的代码更具可测试性,那么我认为您的唯一的选择是创建为此,您自己的 ruby 代码并修改您的 water 副本(请参阅@Dave 的答案),然后准备好在更新 watir 等时维护该自定义补丁。
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.
那么,您对链接的唯一了解就是它带有下划线吗?
我认为这可以做到(使用最新的 watir-webdriver gem):
但我得到了这个:
Jari(watir-webdriver 开发人员)说他认为
u
标签不在 HTML 规范中。顺便说一句,这可行:
Jari 建议尝试 xpath,但我认为 css 选择器会更好读。这里是:
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):
But I got this:
Jari (watir-webdriver developer) said he thinks
u
tag is not in HTML spec.By the way, this works:
Jari suggested trying xpath, but I thought css selectors would be nicer to read. Here it is: