How to Handle this type of popups using selenium with java, tried Using getWindowHandles method but cant figure Out the Problem
CodePudding user response:
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();
CodePudding user response:
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();