Search code examples
ruby-on-railsrubynokogiriwatirruby-watir

Watir - How do I collect all links where the span contains aria_label "Multimedia"


I have written a ruby code where the browser object finds all links and then I store them one by one in an array if they match a specific regex.

@browser.links.collect(&:href).each do |link|
  matches = regex.match(link)
  array_of_multimedia << matches[:multimedia_id] if matches
end

I am trying to create a filter where I only iterate over those links where the span inside the second child div contains the aria-label as Multimedia.

Attached is the screenshot of the HTML structure.HTML structure

I tried a few approaches like finding all spans and then going bottom up to the parent's parent of the span but its not giving me the href.

@browser.spans(aria_label: "Multimedia").each do |span|
 span.parent.parent.a.hreflang #Didn't work
 span.parent.parent.a.link.href #Didn't work
 span.parent.parent.href.text #Didn't work
 element.tag_name #This shows "a" which is correct though
end

I also tried a top down approach by doing

@browser.links.collect(&:href).each do |link|
  link_element = @browser.link(href: link)
  link_element.children.following_sibling(aria_label: "Multimedia").present? #Didn't work
end

So far, no luck in getting the actual hrefs. Will appreciate any help!


Solution

  • Because the span is inside the link tag, it's going to be easier to go bottom up

    Do as much as you can with the Watir locators rather than multiple loops. The parent method takes arguments:

    @browser.spans(aria_label: 'Multimedia').map {|span| span.parent(tag_name: 'a').href }
    

    As for what you tried:

    # parent.parent is the link, so calling `#a` is looking for a link nested inside the link
    span.parent.parent.a.hreflang
    span.parent.parent.a.link.href 
    
    # href should give you a String, you shouldn't need to call #text method on it
    span.parent.parent.href.text 
    
    # element isn't defined here, but try just element.href 
    element.tag_name
    

    Also note that Element#href method is essentially a wrapper for Element#attribute_value('href').