Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Popup's in selenium webdrivers

So I'm working with selenium firefox webdrivers in c# winform and I have this code below to get the handle of the popup that shows when you click on the "webtraffic_popup_start_button" and it should get the handle of the popup but the popup handle is same as current one.

string current = driver.CurrentWindowHandle;
driver.FindElement(By.XPath("//*[@id='webtraffic_popup_start_button']")).Click();
Thread.Sleep(Sleep_Seconds);
popup = driver.CurrentWindowHandle;
Thread.Sleep(3000);
driver.SwitchTo().Window(current);
Thread.Sleep(1000);

Any help with this would be much appreciated thank you

This is what pop up looks like.

Popup_Image

like image 879
Coderz Avatar asked Dec 22 '14 19:12

Coderz


People also ask

How do I switch to popup in Selenium?

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.

How many types of popups are there in Selenium?

Types of Alerts in Selenium There are mainly 3 types of Alerts, namely: Simple Alert. Prompt Alert. Confirmation Alert.

How to handle popups/alerts in Selenium WebDriver?

Let’s see how to handle different types of PopUps / Alerts in Selenium WebDriver one by one. Click on the “Try It” button. It will open the alert box. How to Automate the Scenario? I know you will be fine until clicking on the “Try it” button. But what after the alert box is displayed. How to handle it?

What is pop-up in selenium?

Popup is a window that displays or pops up on the screen due to some activity. If one wishes to know about the various scenarios of pop-ups and how to handle them, read the documentation page. In selenium webdriver, there are multiple methods to handle popups:

How to get the window handle of pop-up window in selenium?

In Selenium, robot class is used to handle the keyboard and mouse functions. It is used to close the pop-up window. You can get the window handle of the pop-up window using the WindowHandle () function. I have created my own webpage to demonstrate popup handling in Selenium.

How to handle multiple windows in Selenium WebDriver?

In Selenium web driver there are methods through which we can handle multiple windows. To handle all opened windows by web driver, we can use "Driver.getWindowHandles ()" and then we can switch window from one window to another in a web application. Its return type is Iterator<String>.


2 Answers

WebDriver does absolutely no tracking whatsoever to detect which window is actually in the foreground in the OS, and does no automatic switching when new browser windows are opened. That means the proper way to get the handle of a newly-opened popup window is a multi-step process. To do so, you would:

  1. Save the currently-focused window handle into a variable so that you can switch back to it later.
  2. Get the list of currently opened window handles.
  3. Perform the action that would cause the new window to appear.
  4. Wait for the number of window handles to increase by 1.
  5. Get the new list of window handles.
  6. Find the new handle in the list of handles.
  7. Switch to that new window.

In code using the .NET language bindings, that would look something like this:

string currentHandle = driver.CurrentWindowHandle;
ReadOnlyCollection<string> originalHandles = driver.WindowHandles;

// Cause the popup to appear
driver.FindElement(By.XPath("//*[@id='webtraffic_popup_start_button']")).Click();

// WebDriverWait.Until<T> waits until the delegate returns
// a non-null value for object types. We can leverage this
// behavior to return the popup window handle.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
string popupWindowHandle = wait.Until<string>((d) =>
{
    string foundHandle = null;

    // Subtract out the list of known handles. In the case of a single
    // popup, the newHandles list will only have one value.
    List<string> newHandles = driver.WindowHandles.Except(originalHandles).ToList();
    if (newHandles.Count > 0)
    {
        foundHandle = newHandles[0];
    }

    return foundHandle;
});

driver.SwitchTo().Window(popupWindowHandle);

// Do whatever you need to on the popup browser, then...
driver.Close();
driver.SwitchTo().Window(currentHandle);

Alternatively, if you're using the .NET bindings, there's a PopupWindowFinder class in the WebDriver.Support assembly that is specifically designed to do these operations for you. Using that class is much simpler.

// Get the current window handle so you can switch back later.
string currentHandle = driver.CurrentWindowHandle;

// Find the element that triggers the popup when clicked on.
IWebElement element = driver.FindElement(By.XPath("//*[@id='webtraffic_popup_start_button']"));

// The Click method of the PopupWindowFinder class will click
// the desired element, wait for the popup to appear, and return
// the window handle to the popped-up browser window. Note that
// you still need to switch to the window to manipulate the page
// displayed by the popup window.
PopupWindowFinder finder = new PopupWindowFinder(driver);
string popupWindowHandle = finder.Click(element);

driver.SwitchTo().Window(popupWindowHandle);

// Do whatever you need to on the popup browser, then...
driver.Close();

// Switch back to parent window
driver.SwitchTo().Window(currentHandle);
like image 198
JimEvans Avatar answered Oct 16 '22 13:10

JimEvans


If the lastly opened window is your target then simply do the following after the click

driver.SwitchTo().Window(driver.WindowHandles.ToList().Last());

EDIT

//You may need to go back to parent window to perform additional actions;

// to the new window
driver.SwitchTo().Window(driver.WindowHandles.ToList().Last());

 // to the new window
driver.SwitchTo().Window(driver.WindowHandles.ToList().First());
//or
driver.SwitchTo().DefaultContent();
like image 45
Saifur Avatar answered Oct 16 '22 14:10

Saifur