7
driver.findElement(By.xpath("//input[@value='添加']")).click(); 
//Pops out an Alert and program stops, does not continue 

how to click the alert?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

啊啊啊啊  怎么没有人呢? (TRANS: ahahahahaha why there is no one here to reply my post?)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我顶  (TRANS: Let me promote this post!)
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
user640778
  • 79
  • 1
  • 1
  • 3
  • I reformatted your code a bit. Is it possible to translate the chinese signs? Maybe they reveal some information. – GvS Mar 02 '11 at 09:15

7 Answers7

12

As of the latest selenium 2 release, this can be done (at least using the FirefoxDriver):

    driver.switchTo().alert().accept();
Lucas
  • 14,227
  • 9
  • 74
  • 124
  • I like this solution, it is working in my code nicely. I am using Firefox, I didn't try this code on other browsers. – Ripon Al Wasim Jul 17 '12 at 07:36
  • where can I see the source code implementation of alert.getAlertText in selenium webdriver source code files? – rahoolm Mar 20 '14 at 01:08
  • @rahoolm check here https://github.com/SeleniumHQ/selenium/blob/ceaf3da79542024becdda5953059dfbb96fb3a90/java/server/src/org/openqa/selenium/remote/server/handler/GetAlertText.java – aaron0207 Sep 06 '17 at 07:56
4

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
Andy Tinkham
  • 1,329
  • 8
  • 9
4

In previous version of Selenium 2 I have had no choice that to handle alerts in Internet Explorer by overriding the window.alert in Javascript:

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
// Override window.alert to store the prompt and accept it automatically
js.ExecuteScript("window.alert = function(msg) { document.bAlert = true; document.lastAlert=msg; }");

// Do some stuff...

// Check for alert
Object o = js.ExecuteScript("return document.bAlert");
if (o != null && (bool)o == true)
{
    //retrieve the alert message
    o = js.ExecuteScript("return document.lastAlert");
    // Do something with the alert text
}

Selenium 2.0b3 has support for handling Alerts in IE and Firefox, so you can do the following:

IAlert alert = driver.SwitchTo().Alert();
// Get the text from the alert
string alertText = alert.Text;
// Accept the alert
alert.Accept();

However, I have not been able to get the above to work with Yes/No alerts (Dismiss() works for No but Accept() doesn't work for Yes). I'm in the process of looking at the IEDriver to work out why this is.

Naishy
  • 1,805
  • 16
  • 15
2

You will have to handle exception and run your handler code for Alert, for Java:

      try{
         driver.findElement(By.xpath("//input[@value='添加']")).click(); 
      } catch(org.openqa.selenium.UnhandledAlertException e){
         Alert alert = driver.switchTo().alert();
         alert.accept();
         // you logic here what you want to do next
      }  

Catch this exception, and then you can accordingly accept or reject alert.

Johnydep
  • 6,027
  • 20
  • 57
  • 74
  • This might work, but it also depends on the purpose of the program. I use Selenium Webdriver for both internal integration testing and crawling the external web. In the testing case this hides an unexpected program state, which I would consider a bad thing - while when crawling the web, it might just save me from someone else's poor usability decision. – Joel Purra Sep 06 '12 at 02:03
2
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://skynet:8081/1.htm");

var selenium = new WebDriverBackedSelenium(driver, driver.Url);
selenium.Start();

selenium.Click("css=input[type=button]");

Assert.AreEqual(selenium.GetConfirmation(), "Are you sure?");
Assert.AreEqual("OK", selenium.GetAlert());

// <input type="button" onclick="if(confirm('Are you sure?')) alert('OK'); else alert('Cancel');" value="Alert test" />

driver.Quit();
iEfimoff
  • 342
  • 4
  • 8
1
Alert alert = driver.switchTo().alert();
alert.accept();

If you want to cancel the pop up use the following:

 alert.dismiss();

instead of

 alert.accept():
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
1

C# code:

IAlert alert = driver.SwitchTo().Alert();
alert.Accept(); 
System.Threading.Thread.Sleep(milliseconds);
JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245