Handling Alert in Selenium WebDriver (Selenium 2)

前端 未结 7 1252
后悔当初
后悔当初 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 04:50
    Alert alert = driver.switchTo().alert();
    alert.accept();
    

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

     alert.dismiss();
    

    instead of

     alert.accept():
    
    0 讨论(0)
  • 2020-12-16 05:08

    C# code:

    IAlert alert = driver.SwitchTo().Alert();
    alert.Accept(); 
    System.Threading.Thread.Sleep(milliseconds);
    
    0 讨论(0)
  • 2020-12-16 05:09
    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();
    
    0 讨论(0)
  • 2020-12-16 05:10

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

        driver.switchTo().alert().accept();
    
    0 讨论(0)
  • 2020-12-16 05:12

    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.

    0 讨论(0)
  • 2020-12-16 05:13

    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.

    0 讨论(0)
提交回复
热议问题