CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-seleniumhq-selenium--selenium-devtools-v129

Chrome DevTools Protocol version 129 bindings for Selenium WebDriver.

Pending
Overview
Eval results
Files

target.mddocs/

Target Management

The v129Target class provides comprehensive target and session management capabilities through the Chrome DevTools Protocol. It enables tab management, debugging session control, and browser context operations.

Capabilities

Target Domain

Core target domain for browser tab and session management.

/**
 * Target domain implementation for tab and session management
 * Implements the idealized Target interface
 */
public class v129Target implements org.openqa.selenium.devtools.idealized.target.Target {
    public v129Target();
}

Target Information

Retrieve information about available targets (tabs, workers, etc.).

/**
 * Gets list of all available targets with their information
 * @return Command returning List of TargetInfo objects
 */
public Command<List<org.openqa.selenium.devtools.idealized.target.model.TargetInfo>> getTargets();

Session Management

Attach to and detach from debugging targets.

/**
 * Attaches to a target for debugging
 * @param targetId - ID of the target to attach to
 * @return Command returning SessionID for the attached session
 */
public Command<SessionID> attachToTarget(TargetID targetId);

/**
 * Detaches from a debugging 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);

Auto-Attach Configuration

Configure automatic attachment to new targets.

/**
 * Sets auto-attach behavior for new targets
 * Automatically attaches to new pages and frames
 * @return Command to configure auto-attach
 */
public Command<Void> setAutoAttach();

Target Events

Monitor target lifecycle events.

/**
 * Event fired when a target is detached
 * @return Event for target detachment notifications
 */
public Event<TargetID> detached();

Usage Examples:

import org.openqa.selenium.devtools.v129.v129Target;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.idealized.target.model.TargetInfo;
import org.openqa.selenium.devtools.idealized.target.model.TargetID;
import org.openqa.selenium.devtools.idealized.target.model.SessionID;

// Create target domain
DevTools devTools = driver.getDevTools();
v129Target target = new v129Target();

// Set up auto-attach for new targets
devTools.send(target.setAutoAttach());

// Listen for target detachment
devTools.addListener(target.detached(), targetId -> {
    System.out.println("Target detached: " + targetId);
});

// Get all available targets
List<TargetInfo> targets = devTools.send(target.getTargets());

for (TargetInfo info : targets) {
    System.out.println("Target: " + info.getTitle() + 
                      " (" + info.getType() + ") - " + info.getUrl());
    
    // Attach to specific targets
    if (info.getType().equals("page") && info.getUrl().contains("example.com")) {
        SessionID sessionId = devTools.send(target.attachToTarget(info.getTargetId()));
        System.out.println("Attached to target with session: " + sessionId);
        
        // Use the session for debugging...
        
        // Detach when done
        devTools.send(target.detachFromTarget(Optional.of(sessionId), Optional.empty()));
    }
}

Advanced Target Management

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

// Track active debugging sessions
Map<TargetID, SessionID> activeSessions = new ConcurrentHashMap<>();

// Monitor new targets and auto-attach
devTools.addListener(target.detached(), targetId -> {
    SessionID session = activeSessions.remove(targetId);
    if (session != null) {
        System.out.println("Cleaning up session: " + session + " for target: " + targetId);
    }
});

// Periodic target discovery
public void discoverTargets() {
    List<TargetInfo> currentTargets = devTools.send(target.getTargets());
    
    for (TargetInfo info : currentTargets) {
        TargetID targetId = info.getTargetId();
        
        // Check if we should attach to this target
        if (shouldAttachToTarget(info) && !activeSessions.containsKey(targetId)) {
            try {
                SessionID sessionId = devTools.send(target.attachToTarget(targetId));
                activeSessions.put(targetId, sessionId);
                
                System.out.println("Attached to new target: " + info.getTitle());
                configureTargetSession(sessionId, info);
                
            } catch (Exception e) {
                System.err.println("Failed to attach to target: " + e.getMessage());
            }
        }
    }
}

private boolean shouldAttachToTarget(TargetInfo info) {
    // Attach to page targets, but not service workers or extensions
    return "page".equals(info.getType()) && 
           !info.getUrl().startsWith("chrome-extension://") &&
           info.getAttached() == false;
}

// Bulk detachment
public void detachFromAllTargets() {
    for (Map.Entry<TargetID, SessionID> entry : activeSessions.entrySet()) {
        try {
            devTools.send(target.detachFromTarget(
                Optional.of(entry.getValue()), 
                Optional.of(entry.getKey())
            ));
        } catch (Exception e) {
            System.err.println("Failed to detach from target: " + e.getMessage());
        }
    }
    activeSessions.clear();
}

Target Filtering and Selection

// Filter targets by criteria
public List<TargetInfo> findTargetsByUrl(String urlPattern) {
    List<TargetInfo> allTargets = devTools.send(target.getTargets());
    
    return allTargets.stream()
        .filter(info -> info.getUrl().matches(urlPattern))
        .collect(Collectors.toList());
}

public Optional<TargetInfo> findTargetByTitle(String title) {
    List<TargetInfo> allTargets = devTools.send(target.getTargets());
    
    return allTargets.stream()
        .filter(info -> title.equals(info.getTitle()))
        .findFirst();
}

// Target type filtering
public List<TargetInfo> getPageTargets() {
    List<TargetInfo> allTargets = devTools.send(target.getTargets());
    
    return allTargets.stream()
        .filter(info -> "page".equals(info.getType()))
        .collect(Collectors.toList());
}

Types

Target Model Types

// From idealized target model
import org.openqa.selenium.devtools.idealized.target.model.TargetID;
import org.openqa.selenium.devtools.idealized.target.model.SessionID;
import org.openqa.selenium.devtools.idealized.target.model.TargetInfo;

// From idealized browser model
import org.openqa.selenium.devtools.idealized.browser.model.BrowserContextID;

Target Information Structure

// TargetInfo contains comprehensive target details
class TargetInfo {
    public TargetInfo(
        TargetID targetId,
        String type,
        String title,
        String url,
        boolean attached,
        Optional<TargetID> openerId,
        Optional<BrowserContextID> browserContextId
    );
    
    public TargetID getTargetId();               // Unique target identifier
    public String getType();                     // Target type (page, worker, etc.)
    public String getTitle();                    // Target title/name
    public String getUrl();                      // Target URL
    public boolean getAttached();                // Whether target is already attached
    public Optional<TargetID> getOpenerId();     // ID of target that opened this one
    public Optional<BrowserContextID> getBrowserContextId(); // Browser context
}

Identifier Types

// Target and Session identifiers
class TargetID {
    public TargetID(String value);
    public String toString();
}

class SessionID {
    public SessionID(String value);
    public String toString();
}

class BrowserContextID {
    public BrowserContextID(String value);
    public String toString();
}

CDP Target Types

// From v129.target.model package
import org.openqa.selenium.devtools.v129.target.model.TargetInfo as CdpTargetInfo;
import org.openqa.selenium.devtools.v129.target.model.TargetID as CdpTargetID;
import org.openqa.selenium.devtools.v129.target.model.SessionID as CdpSessionID;

// Target commands from v129.target package
import org.openqa.selenium.devtools.v129.target.Target;

Command and Event Types

import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.Event;
import org.openqa.selenium.devtools.ConverterFunctions;
import java.util.Map;
import java.util.List;
import java.util.Optional;

Install with Tessl CLI

npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-devtools-v129

docs

domains.md

events.md

index.md

javascript.md

logging.md

network.md

target.md

tile.json