How to handle Pop-up in Selenium WebDriver using Java
Asked Answered
G

10

25

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.

Gamophyllous answered 16/10, 2013 at 12:52 Comment(1)
Take the list of windows by using driver.getWindowHandles().Insurrectionary
G
45

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
Garald answered 16/10, 2013 at 16:46 Comment(4)
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 .Gamophyllous
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.Garald
Tried but code is not working on link : rediff.com -> click on sign in link and try to signed inGamophyllous
@Johnp2: please don't hijack the answerGrueling
G
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();
    }  
}
Gamophyllous answered 17/10, 2013 at 7:8 Comment(0)
A
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();
Ares answered 31/1, 2015 at 7:24 Comment(0)
C
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();
Cyclorama answered 25/3, 2016 at 9:47 Comment(1)
You can use the above code inside your code when you get any web browser pop up alert message box.Cyclorama
W
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");
Womera answered 16/10, 2013 at 13:11 Comment(6)
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: rediff.comGamophyllous
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.Womera
yeh i need to access that and signed inGamophyllous
what is error you are getting while clicking on sign-in link? Any permission error?Womera
didn't get any error but not able to handle email address fieldGamophyllous
Are you getting no such element exception ?Womera
W
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.

Weimaraner answered 28/7, 2015 at 8:58 Comment(0)
E
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
Emmert answered 17/11, 2018 at 3:0 Comment(0)
B
0

It isn't Java, but if the comparison helps, here is a simple Python solution in Chrome that handles the question in a single line.

from selenium import webdriver

driver = webdriver.Chrome()
driver.get(url)

# Save a list of all open window handles
window_handles = driver.window_handles

# Open new window with click() action - modify as needed.
get_element(driver, By.XPATH, "//input[@title]").click()

# Get and switch to the pop-up window
new_popup_window = list(set(driver.window_handles) - set(window_handles))[0]
driver.switch_to.window(new_popup_window)
Barbital answered 16/9, 2023 at 17:59 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Broider
D
-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();
Defer answered 20/11, 2014 at 11:46 Comment(0)
L
-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("[email protected]");
        WebElement ContinueButton=fc.findElement(By.xpath(".//*[@id='contibutton']"));
        ContinueButton.click();

}
Lazurite answered 16/12, 2015 at 7:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.