❅
❅
❆
❅
❆
❅
❆
❅
❆
❆

  • 0939206009
  • thaian.it15@gmail.com
  • Facebook
  • Youtube
  • Zalo
Anh Tester Logo
  • Khoá học
    • All Courses
    • Website Testing
    • API Testing
    • Desktop Testing
    • Mobile Testing
    • Programming Language
    • CI/CD for Tester
    • Performance Testing
  • 💥Khai giảng
  • tools
    • TestGenAI - AI Test Cases Generator
    • Mobile Apps Demo
    • Automation Framework Selenium TestNG
    • Automation Framework Cucumber TestNG
    • Gherkin Convert Functions in Katalon
    • Convert object from Selenium Java to Playwright Python
    • Website Demo CRM
    • Website Demo HRM
    • Website Demo HRM GO
    • Website Demo POS
    • Website Demo eCommerce CMS
  • blog
    • Selenium C#
    • Selenium Java
    • Katalon Tools
    • Jenkins CI/CD
    • SQL cho Tester
    • Manual Testing
    • Tài liệu cho Tester
    • Automation Testing
    • akaAT Tools
    • Cucumber TestNG
    • API Testing with Postman
    • Apache Maven
    • AI in Software Testing
    • Lịch khai giảng
  • Liên hệ
  • Log in
    Sign up

Handle Ads Google using Selenium

  • Blog
  • Automation Testing
Handle Ads Google using Selenium
Video

Handle Ads Google using Selenium

  • Anh Tester
  • Automation Testing
  • 3458
Anh Tester chia sẻ cho các bạn cách xử lý Ads của Google với Selenium

Anh Tester sử dụng code Selenium với Java. Các bạn dùng ngôn ngữ khác chỉnh lại cho phù hợp nhen.

Account An dùng làm demo là có 20 ngày thôi. Có gì các bạn lên trang Guru tạo nhen. Dễ lắm. Hoặc kiếm trang website nào khác test cho trực quan.

@Test
public void testCloseAdsGoogle() {
    try {
        driver.get("https://demo.guru99.com/V4/");
        Thread.sleep(2000);
        driver.findElement(By.name("uid")).sendKeys("mngr484782");
        driver.findElement(By.name("password")).sendKeys("nEzarEs");
        driver.findElement(By.name("btnLogin")).click();
        Thread.sleep(2000);
        driver.findElement(By.linkText("New Customer")).click();
        Thread.sleep(2000);

        //Get element in frame by ID
        WebElement frame1 = driver.findElement(By.id("google_ads_iframe_/24132379/INTERSTITIAL_DemoGuru99_0"));
        //Switch to frame with element
        driver.switchTo().frame(frame1);
        //Check button X or Close displays
        List < WebElement > checkButtonX = driver.findElements(By.xpath("//div[@id='dismiss-button']"));
        System.out.println("checkButtonX: " + checkButtonX.size());
        if (checkButtonX.size() > 0) {
            driver.findElement(By.xpath("//div[@id='dismiss-button']")).click();
        } else {
            WebElement frame2 = driver.findElement(By.id("ad_iframe"));
            driver.switchTo().frame(frame2);
            Thread.sleep(1000);
            List < WebElement > checkButtonClose = driver.findElements(By.xpath("//div[@id='dismiss-button']//span[normalize-space()='Close']"));
            System.out.println("checkButtonClose: " + checkButtonClose.size());
            if (checkButtonClose.size() > 0) {
                driver.findElement(By.xpath("//div[@id='dismiss-button']//span[normalize-space()='Close']")).click();
            } else {
                driver.findElement(By.xpath("//div[@id='dismiss-button']")).click();
            }
        }

        driver.switchTo().defaultContent();
        Thread.sleep(2000);
    } catch (Exception e) {
        e.printStackTrace();
    }
}​


Handle Ads Google using Selenium | Anh Tester


Cách tắt Ads của Google với Selenium


- Bước 1: kiểm tra lấy cái ID của thẻ iframe chứa Ads. Mỗi một Ads trên website đều có ID và ID này không đổi khi chạy lại nên các bạn an tâm.

Handle Ads Google using Selenium | Anh Tester

- Bước 2: Chuyển hướng vào cái iframe đó

//Get element in frame by ID
WebElement frame1 = driver.findElement(By.id("google_ads_iframe_/24132379/INTERSTITIAL_DemoGuru99_0"));
//Switch to frame with element
driver.switchTo().frame(frame1);

- Bước 3: Bắt hết các element cần thiết. Tại đây toàn ID dễ lấy vô cùng nên chắc An không cần chỉ rồi 😄

Handle Ads Google using Selenium | Anh Tester

- Bước 4: Muốn tắt cái Ads đó thì bạn nhấn button X hoặc button Close.

Yeah trên hình là chỉ có button X. Nhưng khi các bạn vào lại lúc khác thì nó lại hiện theo kiểu button Close chính vì thế mình phải đặt điều kiện để kiểm tra từng trường hợp rồi click button cho phù hợp kẻo lỗi do không tìm thấy element.

Code bên trên có note nên các bạn nhìn chắc hiểu rồi 😁


Source code on GitHub:
https://github.com/anhtester/NewFeatureSelenium4/blob/main/src/test/java/CloseAdsGoogle.java

  • Tags:
  • Selenium
  • Ads Google
  • Tips and Tricks

Chia sẻ bài viết

Facebook Linkedin Telegram Pinterest Share with Zalo Zalo

Cộng đồng Automation Testing Việt Nam

🌱 Facebook Fanpage: Anh Tester
🌱 Telegram
Automation Testing:   Cộng đồng Automation Testing
🌱 
Facebook Group Automation: Cộng đồng Automation Testing Việt Nam
🌱 Telegram
Manual Testing:   Cộng đồng Manual Testing
🌱 
Facebook Group Manual: Cộng đồng Manual Testing Việt Nam

  • Anh Tester

    Anh Tester

    Đường dẫu khó chân vẫn cần bước đi
    Đời dẫu khổ tâm vẫn cần nghĩ thấu

    • Facebook
    • Youtube
    • Zalo

Search Blogs

Related Blogs

🤖 So sánh Playwright JS/TS và Playwright Python

🤖 So sánh Playwright JS/TS và Playwright Python

Aug-29-2025 by Anh Tester
So sánh Playwright và Selenium trong Test Automation

So sánh Playwright và Selenium trong Test Automation

Aug-23-2025 by Anh Tester
🚀 Lộ trình học Automation Tester tại Anh Tester

🚀 Lộ trình học Automation Tester tại Anh Tester

Aug-23-2025 by Anh Tester
Những kỹ năng cần có cho level Senior Automation Tester

Những kỹ năng cần có cho level Senior Automation Tester

Dec-17-2024 by Anh Tester
Những kỹ năng cần có cho level Middle Automation Tester

Những kỹ năng cần có cho level Middle Automation Tester

Dec-17-2024 by Anh Tester
Những kỹ năng cần có cho level Junior Automation Tester

Những kỹ năng cần có cho level Junior Automation Tester

Dec-17-2024 by Anh Tester
Những kỹ năng cần có cho level Fresher Automation Tester

Những kỹ năng cần có cho level Fresher Automation Tester

Dec-17-2024 by Anh Tester
Cách xử lý các Exceptions trong Selenium WebDriver

Cách xử lý các Exceptions trong Selenium WebDriver

May-31-2024 by Anh Tester
Tại sao chúng ta cần Kiểm thử Tự động

Tại sao chúng ta cần Kiểm thử Tự động

May-30-2024 by Anh Tester
How to get HTML5 validation message with Selenium

How to get HTML5 validation message with Selenium

Mar-22-2024 by Anh Tester
view all

Blog Tags

  • Selenium
  • Xpath
  • Locator
  • Jenkins
  • Testing
  • Tester
  • Thuật ngữ
  • Lộ trình
  • Khóa học
  • Mindset
  • QA
  • QC
  • Checklist
  • Website
  • Mobile
  • Question
  • Answer
  • Phỏng vấn
  • Extension
  • Cucumber
  • Gherkin
  • Agile
  • Scrum
  • Document
  • Testing Level
  • Automation Test
  • Test Cases
  • Trường hợp
  • Katalon
  • JMeter
  • Postman
  • API
  • Manual Test
  • Developer

Anh Tester

Anh Tester profile
Đường dẫu khó chân vẫn cần bước đi
Đời dẫu khổ tâm vẫn cần nghĩ thấu

Connect me on

  • Facebook
  • Youtube
  • Zalo


Liên hệ

  • 0939206009
  • thaian.it15@gmail.com
  • Anh Tester
  • Donate for Anh Tester
QR Facebook Group
QR Discord Group

Copyright © 2021-2025 Anh Tester Automation Testing