How to close Google vignette (Google ads) that is randomly popping using Selenide

306 Views Asked by At

I had several Selenide automated test web sites which have Google Vignette. The google vignette have different iframe id. I cannot close it using automation code due to different google vignette button and different iframe id.

enter image description here

What is the solution here? Somebody suggest walking through the blur() styling but how? Any other solutions? Thanks.

1

There are 1 best solutions below

0
On

You can wait with retries for google ads via JS script and remove it from DOM if it appeared.

Locator for Google Ads looks like [id*=google_ads_iframe],[id*=ad_iframe]

So, you call querySelector on this element. If it exists - remove it, else retry after 1 second.

function waitForElementAndRemove() {
  let element = document.querySelector('[id*=google_ads_iframe],[id*=ad_iframe]');

  if (element) {
    element.remove();
    console.log('Removed ad');
  } else {
    setTimeout(waitForElementAndRemove, 1000);
  }
}

waitForElementAndRemove();

 public void removeElementAsync(String cssLocator) {
        String observerScript = String.format("function waitForElementAndRemove() {" +
                "  let element = document.querySelector('%s');" +
                "  if (element) {" +
                "    element.remove();" +
                "    console.log('Removed ad');" +
                "  } else {" +
                "    setTimeout(waitForElementAndRemove, 1000);" + 
                "  }" +
                "}" +
                "waitForElementAndRemove();", cssLocator);
        Selenide.executeJavaScript(observerScript);
    }

Test resource with google vignette

Selenide.open("https://demo.guru99.com/articles_popup.php#google_vignette");
String adsSelector = "[id*=google_ads_iframe],[id*=ad_iframe]";
removeElementAsync(adsSelector);
ElementsCollection elements = $$(".dropdown a").filter(Condition.visible);
for (int i = 1; i < 5; i++) {
    elements.get(i).click();
    removeElementAsync(adsSelector);
    Selenide.sleep(1000);
}