Chrome DevTools Protocol (CDP) client library for Chrome version 105, providing Java bindings for browser automation and debugging capabilities
—
Target management for handling browser tabs, web workers, iframes, and other browsing contexts. Essential for multi-context automation scenarios and advanced browser control.
Discover and list all available targets in the browser session.
/**
* Initialize target management handler
*/
public V105Target();
/**
* Get list of all available targets
* @return Command returning list of target information
*/
public Command<List<org.openqa.selenium.devtools.idealized.target.model.TargetInfo>> getTargets();Usage Examples:
import org.openqa.selenium.devtools.v105.V105Target;
import org.openqa.selenium.devtools.idealized.target.model.TargetInfo;
import org.openqa.selenium.devtools.idealized.target.model.TargetID;
// Initialize target management
V105Target target = new V105Target();
// Get all targets
List<TargetInfo> targets = devTools.send(target.getTargets());
// Print target information
for (TargetInfo targetInfo : targets) {
System.out.println("Target ID: " + targetInfo.getTargetId());
System.out.println("Type: " + targetInfo.getType());
System.out.println("Title: " + targetInfo.getTitle());
System.out.println("URL: " + targetInfo.getUrl());
System.out.println("Attached: " + targetInfo.getAttached());
System.out.println("---");
}Attach to specific targets to control them independently.
/**
* Attach to a specific target for control
* @param targetId ID of the target to attach to
* @return Command returning session ID for the attached target
*/
public Command<SessionID> attachToTarget(TargetID targetId);
/**
* Detach from a target
* @param sessionId Optional session ID to detach from
* @param targetId Optional target ID to detach from
* @return Command to detach from target
*/
public Command<Void> detachFromTarget(Optional<SessionID> sessionId, Optional<TargetID> targetId);Usage Examples:
import org.openqa.selenium.devtools.idealized.target.model.SessionID;
import java.util.Optional;
// Find a specific target (e.g., a new tab)
List<TargetInfo> targets = devTools.send(target.getTargets());
TargetInfo pageTarget = targets.stream()
.filter(t -> "page".equals(t.getType()) && !t.getAttached())
.findFirst()
.orElse(null);
if (pageTarget != null) {
// Attach to the target
SessionID sessionId = devTools.send(target.attachToTarget(pageTarget.getTargetId()));
System.out.println("Attached with session ID: " + sessionId);
// Work with the target (send commands using the session)
// Detach when done
devTools.send(target.detachFromTarget(Optional.of(sessionId), Optional.empty()));
}Configure automatic attachment to new targets as they are created.
/**
* Set up automatic attachment to new targets
* @return Command to enable auto-attach with flatten=true
*/
public Command<Void> setAutoAttach();Usage Examples:
// Enable auto-attach for new targets
devTools.send(target.setAutoAttach());
// Now when new tabs, workers, or other targets are created,
// they will automatically be attached and available for control
// Listen for new targets being created
target.detached().addListener(targetId -> {
System.out.println("Target detached: " + targetId);
});
// Open a new tab - it will be auto-attached
((JavascriptExecutor) driver).executeScript("window.open('https://example.com')");
// Get updated target list
List<TargetInfo> updatedTargets = devTools.send(target.getTargets());Monitor target lifecycle events for dynamic target management.
/**
* Get target detached event stream
* @return Event stream for target detachment events
*/
public Event<TargetID> detached();Usage Examples:
// Monitor target lifecycle
target.detached().addListener(targetId -> {
System.out.println("Target detached: " + targetId);
// Clean up any resources associated with this target
cleanupTarget(targetId);
});
// Create and manage multiple targets
public class MultiTargetManager {
private final Map<TargetID, SessionID> activeSessions = new ConcurrentHashMap<>();
private final V105Target targetManager;
public MultiTargetManager(DevTools devTools) {
this.targetManager = new V105Target();
// Set up auto-attach
devTools.send(targetManager.setAutoAttach());
// Monitor detachment
targetManager.detached().addListener(this::handleTargetDetached);
}
public void attachToAllPages() {
List<TargetInfo> targets = devTools.send(targetManager.getTargets());
for (TargetInfo target : targets) {
if ("page".equals(target.getType()) && !target.getAttached()) {
SessionID sessionId = devTools.send(targetManager.attachToTarget(target.getTargetId()));
activeSessions.put(target.getTargetId(), sessionId);
System.out.println("Attached to page: " + target.getTitle());
}
}
}
private void handleTargetDetached(TargetID targetId) {
activeSessions.remove(targetId);
System.out.println("Cleaned up session for target: " + targetId);
}
public void detachFromAll() {
activeSessions.forEach((targetId, sessionId) -> {
devTools.send(targetManager.detachFromTarget(
Optional.of(sessionId),
Optional.of(targetId)
));
});
activeSessions.clear();
}
}Work with specific types of targets like service workers, web workers, and iframes.
Usage Examples:
public class AdvancedTargetManagement {
public void manageServiceWorkers(V105Target targetManager, DevTools devTools) {
List<TargetInfo> targets = devTools.send(targetManager.getTargets());
// Find service workers
List<TargetInfo> serviceWorkers = targets.stream()
.filter(t -> "service_worker".equals(t.getType()))
.collect(Collectors.toList());
for (TargetInfo sw : serviceWorkers) {
System.out.println("Service Worker: " + sw.getUrl());
// Attach to service worker for debugging
SessionID sessionId = devTools.send(targetManager.attachToTarget(sw.getTargetId()));
// Service worker specific operations can be performed here
// using the sessionId to send commands to that specific context
// Detach when done
devTools.send(targetManager.detachFromTarget(
Optional.of(sessionId),
Optional.of(sw.getTargetId())
));
}
}
public void manageWebWorkers(V105Target targetManager, DevTools devTools) {
List<TargetInfo> targets = devTools.send(targetManager.getTargets());
// Find web workers
List<TargetInfo> webWorkers = targets.stream()
.filter(t -> "worker".equals(t.getType()))
.collect(Collectors.toList());
for (TargetInfo worker : webWorkers) {
System.out.println("Web Worker: " + worker.getUrl());
// Attach and control web worker
SessionID sessionId = devTools.send(targetManager.attachToTarget(worker.getTargetId()));
// Execute code in worker context, monitor worker events, etc.
}
}
public void findTargetByUrl(V105Target targetManager, DevTools devTools, String urlPattern) {
List<TargetInfo> targets = devTools.send(targetManager.getTargets());
Optional<TargetInfo> matchingTarget = targets.stream()
.filter(t -> t.getUrl().contains(urlPattern))
.findFirst();
if (matchingTarget.isPresent()) {
TargetInfo target = matchingTarget.get();
System.out.println("Found target: " + target.getTitle() + " at " + target.getUrl());
// Attach and work with the specific target
SessionID sessionId = devTools.send(targetManager.attachToTarget(target.getTargetId()));
return sessionId;
}
return null;
}
}// Target management implementation
public class V105Target implements org.openqa.selenium.devtools.idealized.target.Target {
public V105Target();
Command<List<TargetInfo>> getTargets();
Command<SessionID> attachToTarget(TargetID targetId);
Command<Void> detachFromTarget(Optional<SessionID> sessionId, Optional<TargetID> targetId);
Command<Void> setAutoAttach();
Event<TargetID> detached();
}// Idealized target information
public class org.openqa.selenium.devtools.idealized.target.model.TargetInfo {
TargetInfo(TargetID targetId, String type, String title, String url,
Boolean attached, Optional<TargetID> openerId, Optional<BrowserContextID> browserContextId);
TargetID getTargetId();
String getType();
String getTitle();
String getUrl();
Boolean getAttached();
Optional<TargetID> getOpenerId();
Optional<BrowserContextID> getBrowserContextId();
}
// Target identifier
public class org.openqa.selenium.devtools.idealized.target.model.TargetID {
TargetID(String id);
String toString();
}
// Session identifier for attached targets
public class org.openqa.selenium.devtools.idealized.target.model.SessionID {
SessionID(String id);
String toString();
}// Browser context identifier
public class org.openqa.selenium.devtools.idealized.browser.model.BrowserContextID {
BrowserContextID(String id);
String toString();
}// CDP target information
public class org.openqa.selenium.devtools.v105.target.model.TargetInfo {
TargetID getTargetId();
String getType();
String getTitle();
String getUrl();
Boolean getAttached();
Optional<TargetID> getOpenerId();
Optional<BrowserContextID> getBrowserContextId();
}
// CDP target identifier
public class org.openqa.selenium.devtools.v105.target.model.TargetID {
TargetID(String id);
String toString();
}
// CDP session identifier
public class org.openqa.selenium.devtools.v105.target.model.SessionID {
SessionID(String id);
String toString();
}Common target types you may encounter:
Install with Tessl CLI
npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-devtools-v105