2011-07-18 25 views
1

我遇到的問題是我似乎無法找到如何獲取Bowser.text.include?尋找我在程序中早些時候給出的textinclude的價值。如何獲取Browser.text.include?搜索變量? (使用Watir和Ruby)

它似乎從字面上搜索#{textinclude},這顯然不存在。

這是我當前的代碼:


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 

感謝你的幫助。

感謝

回答

1

是從程序中粘貼的確切代碼嗎?

檢查if browser.text.include? " #{textinclude} "周圍的空間並確保它們不會干擾。例如。如果您正在查找的句子實際上以fullstop而非space結尾,則可能會導致include?以我的經驗返回false。

您將需要像Tapio Saarinen所說的.chomp文件,否則在textinclude variable的末尾會得到\n,這是刪除文本的第一步。

所以我想嘗試這個

text_include = gets.chomp 
if browser.text.include?(text_include) 
puts "Found it" 
else 
puts "Didnt find it" 
end 

如果還是失敗,想想其中的文本是在頁面上。如果它位於按鈕的值中,或者頁面上實際上不是text的地方,則text.include?根本找不到它。

P.s.我將你的變量重命名,所以我可以正確閱讀:)

+0

我已經在網站www.google.com上測試了這一點,並搜索了「A faster」這個詞,它出現在我的谷歌主頁的右上角,順便說一下它對我有用。 – anonygoose

+0

....真棒。非常感謝你。 :) – Benjamin

1

試試這個:

if browser.text.include?(textinclude) 

代替

if browser.text.include? " #{textinclude} " 
+0

對不起,不起作用。 – Benjamin

1

嘗試增加.chomp獲取要搜索後()刪除字符串中的換行符(S)對於

+0

對不起,這也行不通。我們知道是否可以將一個變量拉入browser.text.include?命令? – Benjamin

+0

是的,這是可能的。 –

相關問題