Search code examples
htmlrubywatir-webdriverbrowser-automation

Accessing elements of list items within an order list using Watir


I am trying to click a link that is within a list of ordered items.

This is the HTML

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?


Solution

  • 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