'how to get the xpath from popup which is occur in the same window?

I need to send the values to this pop up. this is open with in the same window. when am trying with the normal driver.findelement, its getting error message saying xpath cannot found This is my code

import java.util.concurrent.TimeUnit;

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


public class Homepage_login {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        FirefoxDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);


        //driver.get("http://se.rdmsonline.net/login.aspx");

        driver.get("http://se.rdmsonline.net/");
        driver.findElement(By.xpath("//*[@id='rcmLang_Arrow']")).click();

        driver.findElement(By.xpath("//*[@id='rcmLang_DropDown']/div/ul/li[2]")).click();

        driver.findElement(By.xpath("//*[@id='txtUserName']")).sendKeys(" ");

        driver.findElement(By.xpath("//*[@id='txtPassword']")).sendKeys(" ");

        driver.findElement(By.xpath("//*[@id='btnLogin']")).click();

        driver.findElement(By.xpath("//*[@id='Div1']/div/a")).click();

        driver.findElement(By.xpath("//*[@id='txtUserName']")).sendKeys(" ");

        driver.findElement(By.xpath("//*[@id='txtPassword']")).sendKeys(" ");

        driver.findElement(By.xpath("//*[@id='btnLogin']")).click();

        driver.findElement(By.xpath("//*[@id='rptSec_ctl00_imb']")).click();

        driver.findElement(By.xpath("//*[@id='ctl00_Menu5']")).click();

        driver.findElement(By.xpath("//*[@id='ctl00_cpl1_lnkNewNotification']")).click();

        driver.findElement(By.xpath("//*[@id='ctl00_cplm_dtpDate_dateInput_text']")).sendKeys(" ");



    }

}

this to be send the control inside the pop up.. can any one help me ?



Solution 1:[1]

First you should switch to the popup and then try to find the element.
You can use following code snippet

String parentWindow = driver.getWindowHandle();
Set<String> windowHandles = driver.getWindowHandles();
Iterator<String> iterator = windowHandles.iterator();
while (iterator.hasNext()) {
   String handle = iterator.next();
   if (!handle.contains(parentWindow)) {
                    // Switch to popup 
                    driver.switchTo().window(handle);
                    // Add code to find element 
       }
}
// Switch back to original window
driver.switchTo().window(parentWindow);

This code will work with any number of popups

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ajinkya