Handling Alert in Selenium WebDriver (Selenium 2)

前端 未结 7 1253
后悔当初
后悔当初 2020-12-16 04:26
driver.findElement(By.xpath(\"//input[@value=\'添加\']\")).click(); 
//Pops out an Alert and program stops, does not continue 

how to click the alert

相关标签:
7条回答
  • 2020-12-16 05:17

    In Selenium 2, currently alerts are only handled in the Firefox browser. You don't specify what language you're using for your tests, but here's how to handle an alert using ruby. (This is taken from the Ruby Bindings page on the selenium wiki).

    Javascript Alert/Confirm

    You can use webdriver to handle javascript alert and confirm dialogs. The implementation for both is the same.

    Note: At this time the API is only available in Firefox (or in Firefox using the remote server), and only alert/confirms that are generated post load can be captured.

    require "selenium-webdriver"
    
    driver = Selenium::WebDriver.for :firefox
    driver.navigate.to "http://mysite.com/page_with_alert.html"
    
    driver.find_element(:name, 'element_with_alert_javascript').click
    a = driver.switch_to.alert
    if a.text == 'A value you are looking for'
      a.dismiss
    else
      a.accept
    end
    
    0 讨论(0)
提交回复
热议问题