watir

watir recursively search for an element inside nested iframe/embed/object


I have a need to locate elements, whether within the top-level document, or within nested containers (iframe/embed/object).

I've written a recursive version of the elements method, but during testing, I noticed that for container elements that load and render dynamic content, the elements render just fine, but the dynamic content doesn't seem to be available to watir.

HTML

<embed src="https://example.com/">

Ruby

element.embeds.each do | embed | 

    puts embed.elements( css: '*' ).length

end 

This just gives me 0 elements, when I would expect to be able to see the contents of the embed too.

What am I missing?


Solution

  • Currently, Watir has no special handling for embed elements. However, it seems that Selenium should be able to interact with them if treated like an iframe.

    You can add a custom Watir class/method to treat embed like iframe (specifically the logic for switching to/from inside the embed element):

    module Watir
      class CustomEmbed < IFrame    
      end
    
      class CustomEmbedCollection < IFrameCollection 
      end   
    
      module Container
        def custom_embed(opts = {})
          CustomEmbed.new(self, opts.merge(tag_name: 'embed'))
        end
    
        def custom_embeds(opts = {})
          CustomEmbedCollection.new(self, opts.merge(tag_name: 'embed'))
        end
      end
    end
    

    Then you can use the normal Watir methods:

    browser.custom_embed(src: 'https://example.com/').elements.count
    #=> 13
    
    browser.custom_embed(src: 'https://example.com/').text
    #=> "Example Domain\nThis domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.\nMore information..."
    

    If this works for you, we can see about adding it to the Watir repository.