【问题标题】:How can I get Browser.text.include? to search for a variable? ( using Watir and Ruby )如何获取 Browser.text.include?搜索变量? (使用 Watir 和 Ruby)
【发布时间】:2011-07-19 00:09:13
【问题描述】:

我遇到的问题是我似乎找不到如何获取 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

感谢您的帮助。

谢谢

【问题讨论】:

    标签: ruby variables text find watir


    【解决方案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? 根本找不到它。

    附:我重命名了您的变量,以便我可以正确阅读它:)

    【讨论】:

    • 我已经在网站 www.google.com 上对此进行了测试,并搜索了出现在我的谷歌主页右上角的“A faster”字样,顺便说一下,它对我有用.
    【解决方案2】:

    试试这个:

    if browser.text.include?(textinclude)
    

    而不是

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

    【讨论】:

      【解决方案3】:

      尝试在gets() 之后添加.chomp 以删除要搜索的字符串中的换行符

      【讨论】:

      • 对不起,这也不起作用。我们是否知道是否可以将变量拉入 browser.text.include 中?命令?
      猜你喜欢
      • 2017-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      相关资源
      最近更新 更多