title: How to handle java script pop up box in selenium
body: <p>I created an automation script for login which contains username and password.</p>
  
  <p>I have an excel sheet in which the result is updated as pass if the username and password is correct.</p>
  
  <p>But if the username and password is incorrect one JavaScript pop up box is coming.</p>
  
  <p>I am unable to handle that ok button.</p>
  
  <p>I already tried this code. But I am getting Exception </p>
  
  <pre><code>org.openqa.selenium.WebDriverException: findElement execution failed;
   An open modal dialog blocked the operation 
  (WARNING: The server did not provide any stacktrace information).
  </code></pre>
  
  <p>How to handle open modal dialog box?</p>
  
  <pre><code>Alert alert = driver.switchTo().alert();
  System.out.println(alert.getText());
  alert.accept();
  </code></pre>
  
  <p>Here is my code</p>
  
  <pre><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 &lt; s.getRows(); i++) {
              System.out.println("s.getColumns() = " + s.getColumns());
  
              for (int j = 0; j &lt; 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 &lt; 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();
      }
  
  }
  </code></pre>