1

我正在使用 selenium webdriver 并尝试让页面对象在报告测试失败之前多次查看页面。我的问题是我遇到了一个错误,我的开始救援块没有捕获到该错误。
这是错误

Selenium::WebDriver::Error::NoSuchElementError: 无法定位元素:{"method":"id","selector":"does not exist"}

class ManageMembers < Nav

  def initialize
    super
    ready = false # Used to indicate if all elements expected are present 
    tryCount = 0# A counter to track the number of attempts to 
    while(ready == false)
      puts "initalizing managemembers"
      #things on the side menue 
      begin
        $driver.find_element(:id, "does not exist")
        $driver.find_element(:id, "sidebar")
        $driver.find_element(:id, "sidebar").find_element(:link, "Manage Members")
        $driver.find_element(:id, "sidebar").find_element(:link, "Create Member")
        @sidebar = true
      rescue @sidebar = false
      end
4

2 回答 2

1

将此添加为答案,以便我们可以将此问题标记为已回答。

class ManageMembers < Nav
  def initialize
    super
    ready = false # Used to indicate if all elements expected are present 
    tryCount = 0# A counter to track the number of attempts to 
    while(ready == false)
      puts "initalizing managemembers"
      #things on the side menue 
      begin
        $driver.find_element(:id, "does not exist")
        $driver.find_element(:id, "sidebar")
        $driver.find_element(:id, "sidebar").find_element(:link, "Manage Members")
        $driver.find_element(:id, "sidebar").find_element(:link, "Create Member")
        @sidebar = true
      rescue Exception => e 
        @sidebar = false
      end
于 2012-11-08T18:57:34.077 回答
0

当您使用块语法 ( begin ... rescue ... end) 而不是内联语法 ( do_something rescue recover_from_something) 时,您需要将救援语句放在下一行。现在你正在混合两种语法风格。

begin
  $driver.find_element(:id, "does not exist")
  $driver.find_element(:id, "sidebar")
  $driver.find_element(:id, "sidebar").find_element(:link, "Manage Members")
  $driver.find_element(:id, "sidebar").find_element(:link, "Create Member")
  @sidebar = true
rescue
  @sidebar = false
end

begin ... end如果您完全拯救一个块(使其成为一个完整的语句),这并不适用,但这看起来很糟糕,我建议使用完整的块语法:

begin
  $driver.find_element(:id, "does not exist")
  $driver.find_element(:id, "sidebar")
  $driver.find_element(:id, "sidebar").find_element(:link, "Manage Members")
  $driver.find_element(:id, "sidebar").find_element(:link, "Create Member")
  @sidebar = true
end rescue @sidebar = false

再一次,不要做这最后一点;)

于 2012-11-08T19:10:33.043 回答