使用Watir访问订单列表中的列表项的元素 [英] Accessing elements of list items within an order list using Watir
问题描述
我尝试点击订单商品列表中的链接。 $ b
该列表是观看次数最多的视频的顺序。
我只是想选择第一个列表项目,所以浏览器将导航到href。
现在,我有
$ website.element(: gs-c-promo-heading nw-o-link gs-o-bullet__text gs-o-faux-block-link__overlay-link gel-pica-bold gs-u-pl @ xs)。click
与另一行代码类似,我必须选择最上面的故事,效果很好。任何想法,为什么这是行不通的?
另外,我将如何抓住这个有序的列表中的所有项目,并打印出每个链接文本?
我无法重现您的例外情况。然而,考虑到例外情况,答案将是去除复合类的名称 - 即在类定位符中不能有任何空格。
大多数类看起来都是通用的,所以可能会被删除。您可以尝试:
$ website.element(:class =>gs-c-promo-heading)。
如果类真的很重要,可以使用Watir的新多类定位器。这会构建一个大的XPath,它不会触发Selenium的复合类检查器。
$ website.element(:class => [ gs-c-promo-heading,nw-o-link,gs-o-bullet__text,gs-o-faux-block-link__overlay-link,gel-pica-bold,gs点击
要获取所有项目,我会找到 li
元素基于它们的 data-entityid
属性,它看起来是描述性的:
$ website.lis(data_entityid:/most-popular-watched/).each do | li |
puts li.link.text
end
这也可能是更多描述性地点击第一个链接:
$ website.li(data_entityid:/most-popular-watched/).link。点击
I am trying to click a link that is within a list of ordered items.
The list is a order of most watched videos.
I'm just trying to select the first list item, so that the broswer will navigate to the href.
Right now, i have
$website.element(:class => "gs-c-promo-heading nw-o-link gs-o-bullet__text gs-o-faux-block-link__overlay-link gel-pica-bold gs-u-pl@xs").click
Which is similar to another line of code i have to select the top story, works fine. Any ideas why this isn't working?
Also, how would i grab all the items in this ordered list and print out each link text?
I was not able to reproduce your exception. However, given the exception, the answer would be to remove the compound class name - ie you cannot have any spaces in the class locator.
Most of the classes seem generic, so could likely be removed. You could try:
$website.element(:class => "gs-c-promo-heading").click
If the classes really are important, you can use Watir's new multi-class locator. This builds a large XPath, which would not trigger Selenium's compound class checker.
$website.element(:class => ["gs-c-promo-heading", "nw-o-link", "gs-o-bullet__text", "gs-o-faux-block-link__overlay-link", "gel-pica-bold", "gs-u-pl@xs"]).click
To grab all of the items, I would locate the li
elements based on their data-entityid
attribute which looks descriptive:
$website.lis(data_entityid: /most-popular-watched/).each do |li|
puts li.link.text
end
This might also be a more descriptive way to click the first link:
$website.li(data_entityid: /most-popular-watched/).link.click
这篇关于使用Watir访问订单列表中的列表项的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!