Ruby 如何在使用watir打开链接时处理nil类
我试图打开一个链接,如果它的href已定义,但得到未定义的方法错误。下面是代码Ruby 如何在使用watir打开链接时处理nil类,ruby,watir,watir-webdriver,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
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
对于nil:NilClass,我得到了这个错误=未定义的方法“[]”。我错在哪里
谢谢 我只在href情况下收到错误,其他情况正常。您尝试单击的链接是什么?A href仅为“?type=1”似乎有点不寻常。这是全部还是一部分?如果它是零件,则需要使用正则表达式进行匹配。例如
a(:href,/\?type=1/
)。