如何使用watir

时间:2016-02-05 00:38:24

标签: ruby watir watir-webdriver

我正在尝试打开一个链接,如果它的href被定义但是得到了未定义的方法错误。以下是代码

def click_link(elementName, elementType, browser)
begin
  case elementType #Check the first byte of the response code.
    when 'class' then
      browser.a(:class, elementName).when_present.click
      return ''
    when 'id' then
      browser.a(:id, elementName).when_present.click
      return ''
    when 'value' then
      browser.a(:value, elementName).when_present.click
      return ''
    when 'text' then
      browser.a(:text, elementName).when_present.click
      return ''
    when 'onclick' then
      browser.a(:onclick, elementName).when_present.click
      return ''
    when 'href' then
      browser.p(:class, "create_button").a(:href, "?type=1").when_present.click
      return ''
    else return "link Not Found"
  end
rescue Watir::Exception::UnknownObjectException,
  Watir::Exception::ObjectDisabledException,
  Watir::Exception::ObjectReadOnlyException,
  Watir::Exception::NoValueFoundException,
  Watir::Exception::MissingWayOfFindingObjectException,
  Watir::Exception::UnknownCellException,
  Watir::Exception::NoMatchingWindowFoundException,
  Watir::Exception::NoStatusBarException,
  Watir::Exception::NavigationException,
  Watir::Exception::UnknownFrameException,
  Watir::Exception::MissingWayOfFindingObjectException,
  Selenium::WebDriver::Error::ElementNotVisibleError => e
  errorMessage = "cannot Find Element: " + elementType+ ": " + elementName
  return errorMessage
end

end

我得到这个错误=未定义的方法`[]'为零:NilClass。哪里出错了

谢谢!

0 个答案:

没有答案