cdp4j is a web-automation library for Java. It can be used for automating the use of web pages and for testing web pages. It use Google Chrome DevTools Protocol to automate Chrome/Chromium based browsers.
- Supports full capabilities of the Chrome DevTools Protocol (tip-of-tree)
- Evaluate JavaScript
- Invoke JavaScript function
- Supports native CSS selector engine
- Supports Sizzle 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
Join us in our Gitter chat room.
Oracle & OpenJDK Java 8.
Both the JRE and the JDK are suitable for use with this library.
cdp4j is released under the terms of the MIT License (MIT). You are free to use cdp4j or any of its constituent parts in any other project (even commercial projects) so long as its copyright headers are left intact.
This library is suitable for use in production systems.
To use the official release of cdp4j, please use the following snippet in your pom.xml
file.
Add the following to your POM's <dependencies>
tag:
<dependency>
<groupId>io.webfolder</groupId>
<artifactId>cdp4j</artifactId>
<version>1.1.0</version>
</dependency>
cdp4j-1.1.0.jar - 734 KB
cdp4j-1.1.0-sources.jar - 501 KB
cdp4j has been tested under Windows 10 and Ubuntu, but should work on any platform where a Java & Chrome/Chromium available.
cdp4j can be run in "headless" mode using with Headless Chrome.
# https://askubuntu.com/questions/79280/how-to-install-chrome-browser-properly-via-command-line
sudo apt-get install libxss1 libappindicator1 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
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
sudo yum install google-chrome-stable_current_*.rpm
Test headless Chrome
google-chrome --headless --remote-debugging-port=9222 --disable-gpu
Simple logger for Java (SLF4J) is supported.
- 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.
cdp4j use W3C selector engine which is default selector engine of Chrome/Chromium. Alternatively Sizzle selector engine might be used. Sizzle is the css selector engine of JQuery and it supports extra selectors like :has(div), :text, contains(text) etc. Check the Sizzle.java for using sizzle with cdp4j.
import io.webfolder.cdp.Launcher;
import io.webfolder.cdp.session.Session;
import io.webfolder.cdp.session.SessionFactory;
public class HelloWorld {
public static void main(String[] args) {
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);
}
}
}
import static java.awt.Desktop.getDesktop;
import static java.awt.Desktop.isDesktopSupported;
import static java.nio.file.Files.createTempFile;
import static java.nio.file.Files.write;
import java.io.IOException;
import java.nio.file.Path;
import io.webfolder.cdp.Launcher;
import io.webfolder.cdp.session.Session;
import io.webfolder.cdp.session.SessionFactory;
public class Screenshot {
public static void main(String[] args) throws IOException, InterruptedException {
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());
}
}
}
import static java.awt.Desktop.getDesktop;
import static java.awt.Desktop.isDesktopSupported;
import static java.nio.file.Files.createTempFile;
import static java.nio.file.Files.write;
import static java.util.Arrays.asList;
import java.io.IOException;
import java.nio.file.Path;
import io.webfolder.cdp.Launcher;
import io.webfolder.cdp.session.Session;
import io.webfolder.cdp.session.SessionFactory;
public class PrintToPDF {
// Requires Headless Chrome
// https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md
public static void main(String[] args) throws IOException {
Launcher launcher = new Launcher();
Path file = createTempFile("webfolder-linux-setup", ".pdf");
try (SessionFactory factory = launcher.launch(asList("--headless", "--disable-gpu"))) {
String context = factory.createBrowserContext();
try (Session session = factory.create(context)) {
session.navigate("https://webfolder.io/linux?cdp4j");
session.waitDocumentReady();
session.wait(1000);
byte[] content = session
.getCommand()
.getPage()
.printToPDF();
write(file, content);
if (isDesktopSupported()) {
getDesktop().open(file.toFile());
}
}
}
}
}
# Assume that you have `google-chrome` in your $PATH
mvn install
# If you use different version of Google Chrome like Chromium, Chrome Canary,
# then you must explicitly use the `chrome_binary` property to make the code work.
mvn install -Dchrome_binary=/path/to/your/google-chrome
# e.g. For some Linux distribution
mvn install -Dchrome_binary=/usr/lib/chromium-dev/chromium-dev
# e.g. MacOS it may be something like
mvn install -Dchrome_binary=/Applications/Chromium.app/Contents/MacOS/Chromium
# To run the existing tests try
mvn test -Dchrome_binary=/usr/lic/chromium-dev/chromium-dev
cdp4j is regularly built and tested on circleci and AppVeyor.
Please follow cdp4j Contributor's Guide.
cdp4j is an MIT licensed open source project and completely free to use. However, the amount of effort needed to maintain and develop new features for the project is not sustainable without proper financial backing. You can support cdp4j development by buying support package. Please contact us for support packages & pricing.