How to handle modal dialogues in selenium ?

Hi,

I’m trying to automate recharge page of infibeam.com. but unable to handle modal dialogue box ..below are the steps, I’m trying to automate..
1) open infibeam.com
2) scroll down to bottom and click on online recharge
3) enter the details
4) click on view popular recharge
5) click on the any amount to recharge on modal dialogue box
6) click on recharge button

please, reply me with code..

Thank you.

How to handle modal dialogues in selenium ?

Train Asked on August 28, 2014 in Selenium WebDriver.

Please close the issue, if it is resolved.

on September 12, 2014.
Add Comment
  • 4 Answer(s)

    Hi Paso,

    Most of the time you can handle Modal window by not using Switch command, as Modal window is a part of a same webpage and it is not a frame or a separate window.

    I advise you to simple locate the price element and perform a click on it, it should work most probably. If not then we would give it a try.

    Regards,
    Lakshay Sharma

    Professor Answered on August 28, 2014.
    Add Comment

    Hi Lakshay,

    It didn’t work.. I tried almost.. Could you please tell me alternate ? 😛

    Train Answered on September 1, 2014.
    Add Comment

    Hi paso
    Again i Would Suggest You to try Thread.sleep()
    and let me know if it works..:)

    thanks
    ankit

    Vice Professor Answered on September 1, 2014.
    Add Comment

    hi ankit,

    I tried with thread.sleep.. may be it worked.. but just unable to select amount value using xpath.. here’s the code for the above test steps :

     
    import org.apache.log4j.Logger;
     import org.apache.log4j.xml.DOMConfigurator;
     import org.openqa.selenium.By;
     import org.openqa.selenium.JavascriptExecutor;
     import org.openqa.selenium.WebDriver;
     import org.openqa.selenium.firefox.FirefoxDriver;
     import org.openqa.selenium.support.ui.Select;
     import org.testng.annotations.BeforeMethod;
     import org.testng.annotations.Test;
    public class Recharge
     {
    private static Logger Log = Logger.getLogger(Recharge.class.getName());
     public WebDriver driver;
    @BeforeMethod
     public void Initialize()
     {
     DOMConfigurator.configure("log4j.xml");
     //System.setProperty("webdriver.chrome.driver", "/home/infibeam/Downloads/chromedriver");
     driver = new FirefoxDriver();
     Log.info("Web Driver is Instantiated");
     driver.manage().window().maximize();
    }
    @Test
     public void recharge() throws Exception
     {
    driver.get("http://infibeam.com");
     Log.info("Website is launched");
    JavascriptExecutor js = (JavascriptExecutor)driver;
     for(int sl=0;;sl++)
     {
     if(sl>=10)
     {
     break;
     }
     js.executeScript("window.scrollBy(0,400)","");
     Thread.sleep(1000);
     }
    Thread.sleep(9000);
     driver.findElement(By.linkText("Online Recharge")).click();
     driver.findElement(By.id("options_mobilenumber")).sendKeys("9066332412");
    Thread.sleep(2000);
     Select select = new Select(driver.findElement(By.id("options_operators")));
     select.selectByIndex(1);
    Thread.sleep(2000);
     Select sele = new Select(driver.findElement(By.id("selectRegion")));
     sele.selectByValue("KK");
    Thread.sleep(2000);
     driver.findElement(By.id("viewRechargePlans")).click();
    Thread.sleep(5000);
     driver.findElement(By.xpath(".//*[@id='standardad']/tbody/tr[8]/td[1]/a"));
    Thread.sleep(3000);
     driver.findElement(By.id("submitRecharge")).click();
     }
    

     

    Train Answered on September 1, 2014.
    Add Comment
  • Your Answer

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