Blocking google_vignette ads using selenium and java
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
Post a Comment