在Selenium WebDriver中使用Ruby时,如何接受javascript警报

时间:2014-10-14 19:38:54

标签: javascript ruby selenium selenium-webdriver

我试图在弹出的JavaScript警报中单击“确定”以删除页面上的元素。我有以下内容:

assert element_present?(:css, "#add_foods_1 > table > tbody > tr > td.cf.cc2 > a.track_remove")
@driver.find_element(:css, "#add_foods_1 > table > tbody > tr > td.cf.cc2 > a.track_remove").click
assert_equal "This will delete this food from your foods eaten today.  Click OK to continue or Cancel to return.", close_alert_and_get_its_text() 

调用close_alert_and_get_its_text()的定义代码为:

def close_alert_and_get_its_text(how, what)
  alert = @driver.switch_to().alert()
  alert_text = alert.text
  if (@accept_next_alert) then
    alert.accept()
  else
    alert.dismiss()
  end
  alert_text
  ensure
    @accept_next_alert = true
  end

我很困惑如何只需单击“确定”即可关闭警报。警报上有一个OK和Cancel。

0 个答案:

没有答案