簡體   English   中英

如何獲取 Browser.text.include? 搜索變量? (使用 Watir 和 Ruby )

[英]How can I get Browser.text.include? to search for a variable? ( using Watir and Ruby )

我遇到的問題是我似乎無法找到如何獲取 Bowser.text.include? 查找我在程序前面給出的 textinclude 的值。

它似乎從字面上搜索#{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

感謝你的幫助。

謝謝

嘗試這個:

if browser.text.include?(textinclude)

代替

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

嘗試在 gets() 之后添加.chomp 以刪除要搜索的字符串中的換行符

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

if browser.text.include? " #{textinclude} " 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? 根本找不到。

Ps 我重命名了你的變量,所以我可以正確閱讀它:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM