How can I get Browser.text.include? to search for a variable? ( using Watir and Ruby )
The problem I am having is that I can't seem to find how to get the Bowser.text.include? to look for the value of textinclude that I give it earlier in the program.
It seems to search literally for #{textinclude}, which is obviously not present.
This is my current code:
print( 'Enter your website: ' )
website = gets()
puts( "Testing #{website}" )
print( 'What text do you want to find? ' )
textinclude = ge开发者_JAVA技巧ts()
puts( "Finding #{textinclude}" )
require 'watir'
browser = Watir::IE.new
browser.goto (website)
if browser.text.include? " #{textinclude} "
print("#{textinclude} present")
else
print("text not present")
end
Appreciate your help.
Thanks
Is that your exact code as pasted from your program?
Check the spaces around if browser.text.include? " #{textinclude} "
and make sure those aren't interfering. E.g. if the sentence you're looking for actually ends with a fullstop
rather than a space
it could cause the include?
to return false in my experience.
You will require the .chomp like Tapio Saarinen said, otherwise you will get \n
at the end of your textinclude variable
, removing this is the first step to being able to find your text.
So I would try this
text_include = gets.chomp
if browser.text.include?(text_include)
puts "Found it"
else
puts "Didnt find it"
end
If that still fails, think about where the text is on the page. If it's in a button's value, or somewhere that is otherwise not actually text
on the page, then the text.include?
will not find it at all.
P.s. I renamed your variable so I could read it properly :)
Try this:
if browser.text.include?(textinclude)
instead of
if browser.text.include? " #{textinclude} "
Try adding .chomp after gets() to remove the newline character(s) in the string to be searched for
精彩评论