popup's in selenium webdrivers

0
0

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

  • You must to post comments
0
0

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
.SwitchToWindow(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();
driver
.SwitchToWindow(currentHandle);
  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website