Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check for text inside a <div>?

I am trying to access some text that is located in a DIV.
I need to check to see if the page holds the text so I can return a true or false.
The code I am using is below:

cancel = browser.text.include?("Current Cancelled")
if cancel == true
puts "Line item cancelled"
else
puts "****Line item not cancelled****"
end

But it returns false every time.
Here is a code snippet of what I am looking into:

enter image description here

like image 235
Curtis Miller Avatar asked Dec 12 '22 15:12

Curtis Miller


1 Answers

I'd really recommend using Nokogiri to parse the content.

require 'nokogiri'

doc = Nokogiri::HTML('<div><span class="label">Current</span>Cancelled</div>')
doc.at('//div/span[@class="label"]/../text()').text # => "Cancelled"

(doc.at('//div/span[@class="label"]/../text()').text.downcase == 'cancelled') # => true
!!(doc.at('//div/span[@class="label"]/../text()').text.downcase['cancelled']) # => true

Something like one of the two bottom statements will get you a usable true/false.

like image 96
the Tin Man Avatar answered Jan 07 '23 13:01

the Tin Man