错误的参数个数(1表示0)(ArgumentError)

时间:2011-10-07 02:18:02

标签: ruby watir

我正在尝试使用Watir查找页面表格中的文本。

表格的一部分:

<td class="left"><a class="xoName" name="Basket Case" href="View.aspx?contactID=2D67AD97-7486-4DB9-AB83-A2C76B116618">Basket Case</a>
</td>
<td class="left" onclick="parent.location='View.aspx?contactID=2D67AD97-7486-4DB9-AB83- A2C76B116618'">shop@basketcase.co</td>

这是我使用的代码:

if b1.text(:xpath =>'/html/body/form/div[2]/div/div[3]/div[4]/table/tbody/tr[2]/td[2]/a').include?(@fullName)
  puts "  Test Passed. Found the contact: " + @fullName + ".  Actual Results match Expected Results."
else
  puts "  Test Failed! Could not find contact: " + @fullName
end

以及我收到的错误消息:

C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.3.4/lib/watir-webdriver/browser.rb:94:in `text': wrong number of arguments (1 for 0) (ArgumentError)
    from C:/Ruby192/scripts/watir/highrise-welli.rb:71:in `<main>'

感谢任何帮助

2 个答案:

答案 0 :(得分:0)

我通常尽我所能避免使用xpath,因为它很慢会产生无法读取的代码,并且正确的路径是很棘手的。

考虑创建一个正则表达式并使用它来指定您要查找的元素以及.exists方法。这假设您知道如何识别要搜索的表,并且文本包含在表中的单个单元格中。

targettext = Regexp.new(@fullName)
if browser.table(:how, 'what').cell(:text, targettext).exists? 
  puts 'success message'
else
  puts 'fail message'
end

如果您没有阅读@fullname并且可以进行硬编码,那么您可以跳过一个步骤并指定内联

  if browser.table(:how, 'what').cell(:text, /textyouaresearchingfor/).exists? 

答案 1 :(得分:0)

对于示例/错误对,text方法不接受args。

in `text': wrong number of arguments (1 for 0) (ArgumentError)

一旦说出“参数”,请查看您正在使用的方法。

此外,使用rspec gem意味着编写更少的代码。然后你可以这样做(见下文),而不是滚动你自己的错误处理。

targettext = Regexp.new(@fullName)
browser.table(:how, 'what').cell(:text, targettext).exists?.should == true

现在你只有两行代码。