如果找不到元素,请继续脚本

时间:2016-07-08 22:01:17

标签: ruby selenium selenium-webdriver

我正在等待在Selenium中加载页面,如下所示:

 wait.until {
       comp = driver.find_element(:xpath, "//span[contains(text(), 'New Page']")
       if comp.displayed?
          myVal= "Passed"
       end
 }

如果新页面没有加载,它会给我一个超时错误,并且脚本不会继续。如果找不到元素,我怎样才能继续?

1 个答案:

答案 0 :(得分:0)

以下是从项目修改的示例:

require 'logger'

logger = Logger.new('logfile.log')

begin
  wait = Selenium::WebDriver::Wait.new(:timeout => 10)

  wait.until {
    comp = driver.find_element(:xpath, "//span[contains(text(), 'New Page']")

    if comp.displayed?
      myVal = "Passed"
    end
  }

rescue => e
  # log error
  logger.error "ERROR! => #{e}"
end

# continue with whatevs