Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I created an automation script for login which contains username and password.

I have an excel sheet in which the result is updated as pass if the username and password is correct.

But if the username and password is incorrect one JavaScript pop up box is coming.

I am unable to handle that ok button.

I already tried this code. But i am getting Exception

org.openqa.selenium.WebDriverException: findElement execution failed;
 An open modal dialog blocked the operation 
(WARNING: The server did not provide any stacktrace information).

Please help me how to handle open modal dialog box.

Alert alert = driver.switchTo().alert();
    System.out.println(alert.getText());
    alert.accept();

Here is my code

public class Read {

    public WebDriver driver;

    @BeforeMethod
    public void launch() throws Exception {
        System.setProperty("webdriver.chrome.driver",
                "C:\\Chrome\\chromedriver_win_26.0.1383.0\\chromedriver.exe");
        driver = new ChromeDriver();
    }

    @Test
    public void testImportexport1() throws BiffException, IOException,
            RowsExceededException, WriteException, InterruptedException {

        FileInputStream fis = new FileInputStream("Data//Logindev.xls");
        Workbook w = Workbook.getWorkbook(fis);
        Sheet s = w.getSheet(0);
        String a[][] = new String[s.getRows()][s.getColumns()];

        FileOutputStream fos = new FileOutputStream("Data//Logindev_1.xls");
        WritableWorkbook wwb = Workbook.createWorkbook(fos);
        WritableSheet ws = wwb.createSheet("LoginResult", 0);

        System.out.println("s.getRows() = " + s.getRows());

        for (int i = 0; i < s.getRows(); i++) {
            System.out.println("s.getColumns() = " + s.getColumns());

            for (int j = 0; j < s.getColumns(); j++) {
                a[i][j] = s.getCell(j, i).getContents();
                Label l = new Label(j, i, a[i][j]);
                Label l1 = new Label(2, 0, "Result");

                ws.addCell(l);
                ws.addCell(l1);

                System.out.println("Labels Added!!!!!!!!!");
            }
        }
        for (int i = 1; i < s.getRows(); i++) {
            driver.get("any url");

            driver.findElement(By.name("txtUserName")).sendKeys(
                    s.getCell(0, i).getContents());
            driver.findElement(By.name("txtPwd")).sendKeys(
                    s.getCell(1, i).getContents());
            driver.findElement(By.name("btnSignIn")).click();

            Thread.sleep(15000);

            if (driver.findElement(By.linkText("xyz")).isDisplayed()) {
                System.out.println("Element is found");
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                driver.findElement(
                        By.xpath("//*[@id='ctl00_headerContent_lnkLogOut']"))
                        .click();
                Thread.sleep(2000);
                Label l2 = new Label(2, i, "Pass");
                ws.addCell(l2);
            } else {

                try {
                    System.out.println("Element Not Found");
                    Label l2 = new Label(2, i, "Fail");
                    ws.addCell(l2);
                    Alert alert = driver.switchTo().alert();
                    System.out.println(alert.getText());
                    alert.accept();

                } catch (NoAlertPresentException e) {

                    e.printStackTrace();
                }

            }
        }
        Thread.sleep(2000);
        wwb.write();
        wwb.close();

    }

}
share|improve this question
    
As it is a modal dialog, there is a possibility that the alert box is residing in a frame. try to switch to the frame and then click OK. –  HemChe Mar 25 '13 at 7:19
    
how will i find the frame name or frame id??Because i am unable to right click on that frame that also on that Ok button.And the firebug is also not identifying on that. –  sarmila Mar 25 '13 at 7:35
    
if you are unable to right clik on the ok button, you can open firebug and search with the text present on the modal box using the find option in firebug. That will show you the html code of your alert box and frame. If you are still unable to find the id, let me know the website if it is not confidential. I'll try to figure out the solution. –  HemChe Mar 25 '13 at 8:00
    
hi HemChe,Still i am unable to find the id by using firebug.But my using site is confidential.Please tell me how will i solve? –  sarmila Mar 25 '13 at 9:32
    
Is it the same dialog box u get when you click on the login button in www.flipkart.com homepage ? –  HemChe Mar 25 '13 at 10:17

3 Answers 3

I think your code snippet to handle a modal dialog box is correct. I even tried it in c# format for a website it works.

IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.tizag.com/javascriptT/javascriptalert.php");
driver.FindElement(By.XPath("//div/form/input[@value='Confirmation Alert']")).Click();
IAlert alert = driver.SwitchTo().Alert();
Console.WriteLine(alert.Text);
alert.Accept();

What I think wrong in your case is that may be your alert handling code is not immediately after the code line which triggers the modal dialog. From the error message you have given it is evident that you are doing some operation on the web page after the modal dialog is displayed and before handling it.

org.openqa.selenium.WebDriverException: findElement execution failed;
An open modal dialog blocked the operation 

I also want to mention about another method to suppress modal dialogs in c# way. Use SendKeys class of name space System.Windows.Forms and send keyboard entries directly like,

IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.tizag.com/javascriptT/javascriptalert.php");
driver.FindElement(By.XPath("//div/form/input[@value='Confirmation Alert']")).Click();
SendKeys.SendWait("{Enter}");

Update


driver.findElement(By.name("txtUserName")).sendKeys(s.getCell(0, i).getContents());
driver.findElement(By.name("txtPwd")).sendKeys(s.getCell(1, i).getContents());
driver.findElement(By.name("btnSignIn")).click();
try{
    Alert alert = driver.switchTo().alert();
    System.out.println(alert.getText());
    alert.accept();
   }
catch (NoAlertPresentException e) {

      if (driver.findElement(By.linkText("xyz")).isDisplayed()) 
        {
         System.out.println("Element is found");
         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
         driver.findElement(
         By.xpath("//*[@id='ctl00_headerContent_lnkLogOut']")).click();
         Thread.sleep(2000);
         Label l2 = new Label(2, i, "Pass");
         ws.addCell(l2);
        } 
     else 
        {
         System.out.println("Element Not Found");
         Label l2 = new Label(2, i, "Fail");
         ws.addCell(l2);
         } 
      }
share|improve this answer
    
Actually that alert handling code is in else part.The if part is working fine but else part is not working because i am unable to click on that Ok or cancel button.Please tell me what to do. –  sarmila Mar 25 '13 at 9:25
    
Can you share the complete code, it will be helpful in analyzing the flow and to drill down the problem. From what you have shared already, I see no issue in the approach. –  CheryJose Mar 25 '13 at 9:50
    
Try the new code flow that I have updated in my answer, it will work for you. I have slightly changed the logic, I made the try for alert and on NoAlertPresentException it will execute your "If & Else" flow. If there is a Modal dialog, alert code in the Try part will handle it –  CheryJose Mar 26 '13 at 11:58

You need to check for the presence of an alert after pressing the logIn button.

driver.findElement(By.id("logIn")).click();

isAlertPresent();


public void isAlertPresent();  
{
    try
    {
        Alert alert = driver.switchTo ().alert ();
        //alert is present
        System.out.println(alert.getText());
        alert.accept(); 
    }
    catch ( NoAlertPresentExceptionn n)
    {
        //Alert isn't present
        return; 
    }
}
share|improve this answer

I cannot give exact solution, but the following approach may help you.

//method for checking if Alert is present.

public boolean isAlertPresent() 
{ 
    try 
    { 
        driver.switchTo().alert(); 
        return true; 
    }   // try 
    catch (NoAlertPresentException Ex) 
    { 
        return false; 
    }   // catch 
} 

//entering username and password

         //click on Login Button
         if( logout button is displayed )
         {
             //click logout
         }
         else if(isAlertPresent())//we are calling the above mentioned isAlertpresent method where, if it returns true, the below code gets executed.
         {
           alert.accept(); 
           Label l2 = new Label(2, i, "Fail");
           ws.addCell(l2);

         }
    }

Hope it gives some idea. Let us know if you are still facing the issue.

share|improve this answer
    
Hi Hemche..I tried your code.But not resolved the issue.Still facing the same problem.Please help me. –  sarmila Mar 25 '13 at 11:36
    
If possible share/mail me the site link or any other website which is look alike(functionality wise) of your website. I will work on it and get back to you with actual working code. –  HemChe Mar 25 '13 at 11:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.