Showing posts with label switchTo(). Show all posts
Showing posts with label switchTo(). Show all posts

Tuesday, 3 December 2013

How to handle java script alert using WebDriver

WebDriver supports handling  JavaScript alerts using Alert interface.

          // Bring control on already opened alert
               Alert alert = driver.switchTo().alert();

          // Get the text/message of the alert or prompt
               alert.getText(); 
         
         // Click ok button of alert
              alert.accept();

         // Click Cancel button of alert

              alert.dismiss();

Selenium - How to switch to Web Dialog window & back to Parent Browser Window

Steps are as follows -

           1.   Before clicking the link, get the handle id of the browser window.
                 String BrowserParent = driver.getWindowHandle();

           2. After clicking the link;
                 String str = driver.getWindowHandle();
                 driver.switchTo().window(st); // switch to child browser

           3.    Once the operation on the web dialog box is completed.    

                   driver.switchTo().window(BrowserParent);