Popup's in Selenium Webdrivers

How to Handle Popup in Selenium WebDriver

The desired element is within a Modal Dialog Box so to locate/click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

Solution

You need to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.close.js-modal-close. > span.close-icon"))).click()
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='close js-modal-close ']/span[@class='close-icon']"))).click()
  • Note : You have to add the following imports:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC

How to handle Pop-up in Selenium WebDriver using Java

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

Popup's in selenium webdrivers

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);

How do I choose to always allow in the popup notification in Google Chrome with Selenium/java?

// Try using chrome options as below.  
Map<String, Object> pref = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.notifications", 2);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);

// if it does not work , you need to check outside selenium.

How can I click on a popup window in selenium Java

This picture posted looks like a javascript alert.

So the below code should have worked.

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

Maybe it is a wait issue. Try WebDriverWait

Alert alert = new WebDriverWait(driver, 20).until(ExpectedConditions.alertIsPresent());
alert.accept();


Related Topics



Leave a reply



Submit