Sunday, April 5, 2015

Handling Modal Dialog Window in Selenium Webdriver



In this tutorial we are going to see how to handle "Modal Dialog Window"  using Selenium Webdriver

Disadvantage using Selenium

  • Use Can't handle elements which is inside Modal Dialog Window.

Using Robot Class
  • Using Robot Class we can handle Modal Dialog Window.

In this tutorial

  • Open http://vodkabears.github.io/remodal/#
  • Click on Show button
  • In the Modal Dialog Window. Click on Follow button 


Source Code :


import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;


public class robot_demo {

public static void main(String[] args) throws InterruptedException, AWTException {
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://vodkabears.github.io/remodal/#");
Thread.sleep(5000);
driver.findElement(By.linkText("Show")).click();
Thread.sleep(7000);
Robot rb=new Robot();
rb.keyPress(KeyEvent.VK_TAB);  
Thread.sleep(2000);
rb.keyPress(KeyEvent.VK_TAB);  
Thread.sleep(2000);
rb.keyPress(KeyEvent.VK_TAB);  
Thread.sleep(2000);
rb.keyPress(KeyEvent.VK_TAB);  
Thread.sleep(2000);
rb.keyPress(KeyEvent.VK_TAB);  
Thread.sleep(2000);
rb.keyPress(KeyEvent.VK_ENTER);

}
}

Hope you like this post

Thankyou.

8 comments:

  1. we could also use 'keys.chord(keys.tab, keys.enter)'

    why to use robot class when we could use keys method ?

    Is there any advantage by using this ?

    ReplyDelete
  2. No advantage of robot with new Actions class in WebDriver 2.0

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. I am having bootstrap3 modal dialogue. i am able to handle it from mozilla. But in chrome selenium is not at all detecting the pop up. How to resolve this issue

    ReplyDelete
  5. Yes its done but u can use normal findelement with xpath and its working....

    ReplyDelete
  6. No word to say about this blog. It was great and useful for us. Thanks Selenium Training in Pune

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete