Skip to content
cdp4j - Chrome DevTools Protocol for Java (Community Edition)
Java JavaScript HTML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib Added examples Oct 22, 2019
src/main Added examples Oct 22, 2019
.gitignore Added examples Oct 22, 2019
LICENSE Create LICENSE Oct 21, 2019
README.md Fixed style Oct 22, 2019
pom.xml Update README.md Oct 22, 2019

README.md

cdp4j - Chrome DevTools Protocol for Java

Features

  • Supports full capabilities of the Chrome DevTools Protocol (tip-of-tree)
  • Evaluate JavaScript
  • Invoke JavaScript function
  • Supports native CSS selector engine
  • Supports XPath queries
  • Incognito Browsing (private tab)
  • Full page screen capture
  • Trigger Mouse events (click etc...)
  • Send keys (text, tab, enter etc...)
  • Redirect log entries (javascript, network, storage etc...) from browser to slf4j
  • Intercept Network (request & response)
  • Upload file programmatically without third party solutions (does not requires AWT Robot etc...)
  • get & set Element properties
  • Supports Headless Chrome/Chromium
  • Navigate back, forward, stop, reload
  • clear cache, clear cookies, list cookies
  • set & get values of form elements
  • Supports event handling

Supported Java Versions

Oracle/OpenJDK 13.

Download

cdp4j-4.0.1.jar 634 KB

External Dependencies

  • GSON 2.8.x (required)
  • slf4j 1.x (optional for logging)
  • log4j 1.x (optional for logging)

How to build and run samples

# clone the repo
git clone https://github.com/webfolderio/lib-cdp4j.git

# download the cdp4j library to lib folder
curl -L https://github.com/webfolderio/lib-cdp4j/releases/download/4.0.1/cdp4j-4.0.1.jar --output lib/cdp4j-4.0.1.jar

Stability

This library is suitable for use in production systems.

Headless Mode

cdp4j can be run in "headless" mode using with Options.headless(boolean) option.

Install Chrome on Debian/Ubuntu

# https://askubuntu.com/questions/79280/how-to-install-chrome-browser-properly-via-command-line
sudo apt-get install libxss1 libappindicator1 libappindicator3-1 libindicator7
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome*.deb # Might show "errors", fixed by next line
sudo apt-get install -f

Install Chrome on RHEL/CentOS/Fedora

wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
sudo yum install google-chrome-stable_current_*.rpm

Logging

slf4j 1.x, log4j 1.x and custom Console logger is supported.

Design Principles

  • Avoid external dependencies as much as possible.
  • Support only Chrome/Chromium based browsers.
  • Supports full capabilities of the Chrome DevTools Protocol.
  • Keep the API simple.
  • Support GraalVM.
  • Support nio based websocket clients.

JavaDoc

cdp4j api

Usage Examples

Print text content with cdp4j

Launcher launcher = new Launcher();
try (SessionFactory factory = launcher.launch();
                    Session session = factory.create()) {
    session.navigate("https://webfolder.io");
    session.waitDocumentReady();
    String content = (String) session.getProperty("//body", "outerText");
    System.out.println(content);
}

Full page screen capture with cdp4j

Launcher launcher = new Launcher();
Path file = createTempFile("screenshot", ".png");
try (SessionFactory factory = launcher.launch();
                    Session session = factory.create()) {
    session.navigate("https://news.ycombinator.com");
    session.waitDocumentReady();
    // activate the tab/session before capturing the screenshot
    session.activate();
    byte[] data = session.captureScreenshot();
    write(file, data);
}
if (isDesktopSupported()) {
    getDesktop().open(file.toFile());
}

Print to PDF with cdp4j

Launcher launcher = new Launcher();
Options options = Options.builder()
        .headless(true)
    .build();
Path file = createTempFile("webfolder-linux-setup", ".pdf");
try (SessionFactory factory = launcher.launch(options)) {
    String context = factory.createBrowserContext();
    try (Session session = factory.create(context)) {
        session.navigate("https://webfolder.io?cdp4j");
        session.waitDocumentReady();
        byte[] content = session.printToPDF();
        write(file, content);
    }
}

Samples

- - - -
Attributes Bing BingTranslator CheckBox
ExecuteJavascript CaptureDOMSnapshot HelloWorld IncognitoBrowsing
Logging MultiSelect NetworkResponse Screenshot
Select SendKeys SharedSession EvaluateOnNewDocument
UserAgent WaitUntil XPathSelector CodeCoverage
PrintToPDF BasicAuthentication DownloadFile Crawler
MultiProcess FollowRedirects CloseSessionOnRedirect Readability
InvokeJavaFromJs InvokeJsFromJava Profiling PrintPDFtoFile

Community License

If you are a tech enthusiast with the ability to self-support, our Community Edition product is freely available and offer powerful capabilities for non-critical business process. If you need enterprise-class features, scalability and performance, or if you plan to use the software in a production environment that requires support from WebFolder, you should consider cdp4j commercial license.

You can’t perform that action at this time.