How to Handle this type of popups using selenium with java, tried Using getWindowHandles method but cant figure Out the Problem
2 Answers
It is only possible to give a generic answer to your question on how to get rid of an ad popup. Please be aware, that you must make sure to really alway get the same ad popup and this is usually not the case with advertising. Assuming that you always get the exact same (also the implementation) popup, you must first inspect the popup or directly the close button within the popup and find some attributes that can be used to identify the close button element. Let‘s now also assume that the close button can be identified, you are pretty much done and only need to use selenium to select the element (the css selector is just an example) and send a click command.
element = driver.findElement(By.cssSelector("input[value='close']"));
element.click();

- 9,511
- 18
- 74
- 126
-
yes ..but ad popup is always different on every new tab ..so please help to handle this type of ad popups which is different every time – Hari Dhakad Mar 26 '22 at 07:46
-
Obviously there cannot be a solution when advertising providers will do everything to prevent you from clicking the ads away. You can only try to "guess" if there is a popup or continue adding each individual case you find. In practice both is almost impossible. – doberkofler Mar 26 '22 at 10:10
Pop up can be handled with window handlers String mainwindow=driver.getWindowHandle(); // To handle all new opened window.
Set<String> s1=driver.getWindowHandles();
Iterator<String> i1=s1.iterator();
while(i1.hasNext())
{
String ChildWindow=i1.next();
if(!MainWindow.equalsIgnoreCase(ChildWindow))
{
// Switching to Child window
driver.switchTo().window(ChildWindow);
element = driver.findElement(By.cssSelector("input[value='close']"));
element.click()
// Closing the Child Window.
driver.close();
or else we can use alert
Alert confirmationAlert = driver.switchTo().alert();
String alertText = confirmationAlert.getText();
System.out.println("Alert text is " + alertText);
confirmationAlert.accept();