简体   繁体   中英

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 = gets()

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

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

Is that your exact code as pasted from your program?

Check the spaces around if browser.text.include? " #{textinclude} " if browser.text.include? " #{textinclude} " and make sure those aren't interfering. Eg 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.

Ps I renamed your variable so I could read it properly:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM