How to handle java script popup in selenium

I created a login script that contains a username and password.

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

But if the username and password are incorrect, a JavaScript popup will appear.

I cannot process this ok button.

I have already tried this code. But I get an exception

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

How to handle an 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(); } } 
+4
source share
3 answers

I think your code snippet for processing a modal dialog box is correct. I even tried it in C # format for the website it runs on.

 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 is wrong in your case is that maybe your warning code may not immediately after the line of code that launches the modal dialog box. From the error message that you indicated, it is obvious that you are performing some operation on the web page after displaying the modal dialog box and before processing it.

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

I also want to mention another method for suppressing modal dialogs in C #. Use the SendKeys class of the System.Windows.Forms namespace and send the 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); } } 
+1
source

You need to check for a warning after clicking 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; } } 
0
source

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

// method for checking for an alert.

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

// enter 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 this gives some idea. Let us know if you are still experiencing a problem.

0
source

All Articles