├── pom.xml ├── .gitignore ├── README.md ├── LICENSE └── src └── main └── java └── com └── ahmedhasan └── CDPConsoleLogTest.java /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.ahmedhasan 8 | selenium-cdp-console-logger 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 22 13 | 22 14 | UTF-8 15 | 16 | 17 | 18 | org.seleniumhq.selenium 19 | selenium-java 20 | 4.34.0 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ############################## 2 | ## Java 3 | ############################## 4 | .mtj.tmp/ 5 | *.class 6 | *.jar 7 | *.war 8 | *.ear 9 | *.nar 10 | hs_err_pid* 11 | activityLog.log 12 | 13 | ############################## 14 | ## Maven 15 | ############################## 16 | target/ 17 | pom.xml.tag 18 | pom.xml.releaseBackup 19 | pom.xml.versionsBackup 20 | pom.xml.next 21 | pom.xml.bak 22 | release.properties 23 | dependency-reduced-pom.xml 24 | buildNumber.properties 25 | .mvn/timing.properties 26 | .mvn/wrapper/maven-wrapper.jar 27 | 28 | ############################## 29 | ## IntelliJ 30 | ############################## 31 | out/ 32 | .idea/ 33 | .idea_modules/ 34 | *.iml 35 | *.ipr 36 | *.iws 37 | 38 | ############################## 39 | ## Eclipse 40 | ############################## 41 | .settings/ 42 | bin/ 43 | tmp/ 44 | .metadata 45 | .classpath 46 | .project 47 | *.tmp 48 | *.bak 49 | *.swp 50 | *~.nib 51 | local.properties 52 | .loadpath 53 | .factorypath 54 | 55 | ############################## 56 | ## OS X 57 | ############################## 58 | .DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CDP Console Log Capture with Selenium 4.34.0 2 | 3 | A minimal Java example demonstrating how to capture browser console logs using Chrome DevTools Protocol (CDP) with Selenium 4.34.0 and ChromeDriver. 4 | 5 | --- 6 | 7 | ## 🔍 Overview 8 | 9 | This project uses **Chrome DevTools Protocol (CDP)** to tap into the browser’s internal console and listen for JavaScript log events like `console.log`, `console.error` and more — all through **Selenium 4.34.0**. 10 | 11 | Created by **Ahmed Hasan**, 2025. 12 | 13 | --- 14 | 15 | ## 📦 Tech Stack 16 | 17 | - Java 22+ 18 | - Selenium 4.34.0 19 | - ChromeDriver (auto-resolved by Selenium Manager) 20 | - Chrome 138 or later 21 | 22 | --- 23 | 24 | ## 🚀 How It Works 25 | 26 | - Starts a ChromeDriver session 27 | - Attaches a CDP `DevTools` session 28 | - Enables log capture via `Log.enable()` 29 | - Listens for and prints browser console log entries 30 | 31 | --- 32 | 33 | ## 🛠️ Setup Instructions 34 | 35 | ### 1. Clone the Repo 36 | 37 | ```bash 38 | git clone https://github.com/im-ahmed-hasan/selenium-cdp-automation.git 39 | cd cdp-console-log-capture 40 | 41 | 42 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Ahmed Foysol Hasan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/main/java/com/ahmedhasan/CDPConsoleLogTest.java: -------------------------------------------------------------------------------- 1 | package com.ahmedhasan; 2 | 3 | /* 4 | * Author: Ahmed Hasan 5 | * © Ahmed Hasan 2025. All rights reserved. 6 | * 7 | * This example demonstrates how to use Chrome DevTools Protocol (CDP) 8 | * with Selenium 4.34.0 to capture JavaScript console logs in Chrome. 9 | */ 10 | 11 | import org.openqa.selenium.chrome.ChromeDriver; 12 | import org.openqa.selenium.devtools.DevTools; 13 | import org.openqa.selenium.devtools.v138.log.Log; 14 | 15 | public class CDPConsoleLogTest { 16 | 17 | public static void main(String[] args) { 18 | 19 | // Start Chrome browser (Selenium Manager will auto-resolve driver) 20 | ChromeDriver driver = new ChromeDriver(); 21 | 22 | // Create a DevTools session 23 | DevTools devTools = driver.getDevTools(); 24 | devTools.createSession(); 25 | 26 | // Enable browser log capture 27 | devTools.send(Log.enable()); 28 | 29 | // Add listener for log events 30 | devTools.addListener(Log.entryAdded(), logEntry -> { 31 | System.out.println("\n==================== CDP Console Log ===================="); 32 | System.out.printf(" 🔔 Level : %-10s%n", logEntry.getLevel()); 33 | System.out.printf(" 📝 Message : %s%n", logEntry.getText()); 34 | System.out.printf(" 🌐 URL : %s%n", logEntry.getUrl().orElse("N/A")); 35 | System.out.println("========================================================="); 36 | System.out.println(" © Ahmed Hasan · v2025"); 37 | }); 38 | 39 | // Load test page 40 | driver.get("https://example.com"); 41 | 42 | // Wait briefly to capture the event 43 | try { 44 | Thread.sleep(3000); 45 | } catch (InterruptedException e) { 46 | e.printStackTrace(); 47 | } 48 | 49 | // Close the browser 50 | driver.quit(); 51 | } 52 | } 53 | --------------------------------------------------------------------------------