ruby - How to find an element that has a specific text without other inside elements -


i have html string this.

html = '<div>outer<div>inner</div></div>' 

i want text inside of div element.

doc = nokogiri::html(html) doc.xpath('//div[contains(.,"inner")]') 

but code gets not inner element, outer element because outer element contains text inner.

how can find element contains specific text without inner html tag?

i can inner element in case doc.css('div > div'), in real case, not sure how many div tags exist. , inner text may include more text, except inner like:

html = '<div>outer<div>inner text</div></div>' 

the best solution be, if inner element has class or id attribute.

doc.css('.inner') # or doc.css('#inner') 

so prefer use doc#css method, because searching xpath can slow.


Comments