Search code examples
rubywatir

Excluding contents of <span> from text using Waitr


Watir

mytext =browser.element(:xpath => '//*[@id="gold"]/div[1]/h1').text

Html

<h1>
 This is the text I want
 <span> I do not want this text </span>
</h1>

When I run my Watir code, it selects all the text, including what is in the spans. How do I just get the text "This is the text I want", and no span text?


Solution

  • If you have a more complicated HTML, I find it can be easier to deal with this using Nokogiri as it provides more methods for parsing the HTML:

    require 'nokogiri'
    
    h1 = browser.element(:xpath => '//*[@id="gold"]/div[1]/h1')
    doc = Nokogiri::HTML.fragment(h1.html)
    mytext = doc.at('h1').children.select(&:text?).map(&:text).join.strip