24

I want to handle sign-in part in rediff.com, but the below code doesn't work for that:

driver.get("http://www.rediff.com/");
WebElement sign = driver.findElement(By.xpath("//html/body/div[3]/div[3]/span[4]/span/a"));
sign.click();
String myWindowHandle = driver.getWindowHandle();
driver.switchTo().window(myWindowHandle);
WebElement email_id= driver.findElement(By.xpath("//*[@id='signin_info']/a[1]"));
email_id.sendKeys("hi");

If myWindowHandle is not the correct string, then let me know how to get the pop-up Window name, because I can't find the name of the pop-up window.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Niyati
  • 493
  • 3
  • 5
  • 20

9 Answers9

44

To switch to a popup window, you need to use getWindowHandles() and iterate through them.

In your code you are using getWindowHandle() which will give you the parent window itself.

String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;

Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
    subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler); // switch to popup window

// Now you are in the popup window, perform necessary actions here

driver.switchTo().window(parentWindowHandler);  // switch back to parent window
LINGS
  • 3,560
  • 5
  • 34
  • 47
  • But i just need to handle only one window than i think so no meaning to use iterator ...do u have another way . i just need to write email address and password in sign in form of rediff.com .pls helm me in this . – Niyati Oct 17 '13 at 05:09
  • The above code will handle that. Once you switch to sub window which is your popup. Pick the webelements and fill in your email id, password and submit. Then switch back to your parent window. – LINGS Oct 17 '13 at 05:14
  • 1
    Tried but code is not working on link : rediff.com -> click on sign in link and try to signed in – Niyati Oct 17 '13 at 05:58
  • @Johnp2: please don't hijack the answer – Hovercraft Full Of Eels Jun 18 '17 at 02:11
11

I found the solution for the above program, which had the goal of signing in to http://rediff.com

public class Handle_popupNAlert
{
    public static void main(String[] args ) throws InterruptedException
    {
        WebDriver driver= new FirefoxDriver(); 
        driver.get("http://www.rediff.com/");
        WebElement sign = driver.findElement(By.xpath("//html/body/div[3]/div[3]/span[4]/span/a"));
        sign.click();

        Set<String> windowId = driver.getWindowHandles();    // get  window id of current window
        Iterator<String> itererator = windowId.iterator();   

        String mainWinID = itererator.next();
        String  newAdwinID = itererator.next();

        driver.switchTo().window(newAdwinID);
        System.out.println(driver.getTitle());
        Thread.sleep(3000);
        driver.close();

        driver.switchTo().window(mainWinID);
        System.out.println(driver.getTitle());
        Thread.sleep(2000);

        WebElement email_id= driver.findElement(By.xpath("//*[@id='c_uname']"));
        email_id.sendKeys("hi");
        Thread.sleep(5000);

        driver.close();
        driver.quit();
    }  
}
Seanny123
  • 8,776
  • 13
  • 68
  • 124
Niyati
  • 493
  • 3
  • 5
  • 20
5

You can handle popup window or alert box:

Alert alert = driver.switchTo().alert();
alert.accept();

You can also decline the alert box:

Alert alert = driver.switchTo().alert();
alert().dismiss();
smottt
  • 3,272
  • 11
  • 37
  • 44
ER.swatantra
  • 125
  • 1
  • 5
5

You can use the below code inside your code when you get any web browser pop-up alert message box.

// Accepts (Click on OK) Chrome Alert Browser for RESET button.

Alert alertOK = driver.switchTo().alert();
alertOK.accept();



//Rejects (Click on Cancel) Chrome Browser Alert for RESET button.

Alert alertCancel = driver.switchTo().alert();
alertCancel.dismiss();
Sandeep
  • 1,504
  • 7
  • 22
  • 32
2

Do not make the situation complex. Use ID if they are available.

driver.get("http://www.rediff.com");
WebElement sign = driver.findElement(By.linkText("Sign in"));
sign.click();
WebElement email_id= driver.findElement(By.id("c_uname"));
email_id.sendKeys("hi");
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
HemChe
  • 2,249
  • 1
  • 21
  • 33
  • I can''t directly use email_id field as its in pop up window ..pls visit rediff.com as i want to write email address and password in sign in form of rediff.com. link: http://www.rediff.com/ – Niyati Oct 17 '13 at 05:11
  • which browser are you using? Because i am not seeing any pop up window after clicking on sign-in. it is just another modal window (inside main window). i am using firefox23. – HemChe Oct 17 '13 at 05:34
  • yeh i need to access that and signed in – Niyati Oct 17 '13 at 05:38
  • what is error you are getting while clicking on `sign-in` link? Any permission error? – HemChe Oct 17 '13 at 05:48
  • didn't get any error but not able to handle email address field – Niyati Oct 17 '13 at 05:59
  • Are you getting `no such element` exception ? – HemChe Oct 17 '13 at 06:00
0

When the toastr message poped up on the screen of firefox. the below tag was displayed in fire bug.

<div class="toast-message">Invalid Credentials, Please check Password</div>.

I took the screenshot at that time. And did the below changes in selenium java code.

String alertText = "";
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("toast-message")));
WebElement toast1 = driver.findElement(By.className("toast-message"));  
alertText = toast1.getText();
System.out.println( alertText);

And my issue of toastr popup got resolved.

Sandip
  • 1
0
String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;

Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();

subWindowHandler = iterator.next();

driver.switchTo().window(subWindowHandler); // switch to popup window

// Now you are in the popup window, perform necessary actions here

driver.switchTo().window(parentWindowHandler);  // switch back to parent window
Machavity
  • 30,841
  • 27
  • 92
  • 100
-1
       //get the main handle and remove it
       //whatever remains is the child pop up window handle

       String mainHandle = driver.getWindowHandle();
       Set<String> allHandles = driver.getWindowHandles();
       Iterator<String> iter = allHandles.iterator();
       allHandles.remove(mainHandle);
       String childHandle=iter.next();
-1
public void Test(){

     WebElement sign = fc.findElement(By.xpath(".//*[@id='login-scroll']/a"));
        sign.click();
        WebElement LoginAsGuest=fc.findElement(By.xpath(".//*[@id='guest-login-option']"));
        LoginAsGuest.click();
        WebElement email_id= fc.findElement(By.xpath(".//*[@id='guestemail']"));
        email_id.sendKeys("ankushdeoladore@gmail.com");
        WebElement ContinueButton=fc.findElement(By.xpath(".//*[@id='contibutton']"));
        ContinueButton.click();

}
Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33