Blocking google_vignette ads using selenium and java

Steps :
1.full page ads are block through JavaScript executor
2. We are using example website "https://automationexercise.com"
3. We are blocking ads on the product page

Google vignette ads example :



Sample of the code



package com.demo;


import java.util.NoSuchElementException;

import java.util.concurrent.TimeUnit;


import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.Test;


public class GoogleAdsblock {

static WebDriver driver;

@Test

public static void blockads() throws InterruptedException{

System.setProperty("webdriver.chrome.driver", "C:\\bin\\chromedriver.exe");

driver = new ChromeDriver();

driver.get("https://automationexercise.com");

WebElement productBtn = driver.findElement(By.xpath("//i[@class='material-icons card_travel']"));

productBtn.click();

Thread.sleep(1000);

JavascriptExecutor js = (JavascriptExecutor) driver;

//blocking ads

js.executeScript("const elements = document.getElementsByClassName('adsbygoogle adsbygoogle-noablate'); while (elements.length > 0) elements[0].remove()");

js.executeScript("window.scrollBy(0,350)");

driver.findElement(By.xpath("//a[@href='/product_details/1']")).click();

driver.close();

}

}




Any doubts you can ask me in the comments





Comments

Popular posts from this blog

How to open or handle the download page in multiple browsers using Selenium