0
# Target Management
1
2
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.
3
4
## Capabilities
5
6
### Target Domain
7
8
Core target domain for browser tab and session management.
9
10
```java { .api }
11
/**
12
* Target domain implementation for tab and session management
13
* Implements the idealized Target interface
14
*/
15
public class v129Target implements org.openqa.selenium.devtools.idealized.target.Target {
16
public v129Target();
17
}
18
```
19
20
### Target Information
21
22
Retrieve information about available targets (tabs, workers, etc.).
23
24
```java { .api }
25
/**
26
* Gets list of all available targets with their information
27
* @return Command returning List of TargetInfo objects
28
*/
29
public Command<List<org.openqa.selenium.devtools.idealized.target.model.TargetInfo>> getTargets();
30
```
31
32
### Session Management
33
34
Attach to and detach from debugging targets.
35
36
```java { .api }
37
/**
38
* Attaches to a target for debugging
39
* @param targetId - ID of the target to attach to
40
* @return Command returning SessionID for the attached session
41
*/
42
public Command<SessionID> attachToTarget(TargetID targetId);
43
44
/**
45
* Detaches from a debugging target
46
* @param sessionId - Optional session ID to detach from
47
* @param targetId - Optional target ID to detach from
48
* @return Command to detach from target
49
*/
50
public Command<Void> detachFromTarget(Optional<SessionID> sessionId, Optional<TargetID> targetId);
51
```
52
53
### Auto-Attach Configuration
54
55
Configure automatic attachment to new targets.
56
57
```java { .api }
58
/**
59
* Sets auto-attach behavior for new targets
60
* Automatically attaches to new pages and frames
61
* @return Command to configure auto-attach
62
*/
63
public Command<Void> setAutoAttach();
64
```
65
66
### Target Events
67
68
Monitor target lifecycle events.
69
70
```java { .api }
71
/**
72
* Event fired when a target is detached
73
* @return Event for target detachment notifications
74
*/
75
public Event<TargetID> detached();
76
```
77
78
**Usage Examples:**
79
80
```java
81
import org.openqa.selenium.devtools.v129.v129Target;
82
import org.openqa.selenium.devtools.DevTools;
83
import org.openqa.selenium.devtools.idealized.target.model.TargetInfo;
84
import org.openqa.selenium.devtools.idealized.target.model.TargetID;
85
import org.openqa.selenium.devtools.idealized.target.model.SessionID;
86
87
// Create target domain
88
DevTools devTools = driver.getDevTools();
89
v129Target target = new v129Target();
90
91
// Set up auto-attach for new targets
92
devTools.send(target.setAutoAttach());
93
94
// Listen for target detachment
95
devTools.addListener(target.detached(), targetId -> {
96
System.out.println("Target detached: " + targetId);
97
});
98
99
// Get all available targets
100
List<TargetInfo> targets = devTools.send(target.getTargets());
101
102
for (TargetInfo info : targets) {
103
System.out.println("Target: " + info.getTitle() +
104
" (" + info.getType() + ") - " + info.getUrl());
105
106
// Attach to specific targets
107
if (info.getType().equals("page") && info.getUrl().contains("example.com")) {
108
SessionID sessionId = devTools.send(target.attachToTarget(info.getTargetId()));
109
System.out.println("Attached to target with session: " + sessionId);
110
111
// Use the session for debugging...
112
113
// Detach when done
114
devTools.send(target.detachFromTarget(Optional.of(sessionId), Optional.empty()));
115
}
116
}
117
```
118
119
### Advanced Target Management
120
121
```java
122
import java.util.Map;
123
import java.util.concurrent.ConcurrentHashMap;
124
125
// Track active debugging sessions
126
Map<TargetID, SessionID> activeSessions = new ConcurrentHashMap<>();
127
128
// Monitor new targets and auto-attach
129
devTools.addListener(target.detached(), targetId -> {
130
SessionID session = activeSessions.remove(targetId);
131
if (session != null) {
132
System.out.println("Cleaning up session: " + session + " for target: " + targetId);
133
}
134
});
135
136
// Periodic target discovery
137
public void discoverTargets() {
138
List<TargetInfo> currentTargets = devTools.send(target.getTargets());
139
140
for (TargetInfo info : currentTargets) {
141
TargetID targetId = info.getTargetId();
142
143
// Check if we should attach to this target
144
if (shouldAttachToTarget(info) && !activeSessions.containsKey(targetId)) {
145
try {
146
SessionID sessionId = devTools.send(target.attachToTarget(targetId));
147
activeSessions.put(targetId, sessionId);
148
149
System.out.println("Attached to new target: " + info.getTitle());
150
configureTargetSession(sessionId, info);
151
152
} catch (Exception e) {
153
System.err.println("Failed to attach to target: " + e.getMessage());
154
}
155
}
156
}
157
}
158
159
private boolean shouldAttachToTarget(TargetInfo info) {
160
// Attach to page targets, but not service workers or extensions
161
return "page".equals(info.getType()) &&
162
!info.getUrl().startsWith("chrome-extension://") &&
163
info.getAttached() == false;
164
}
165
166
// Bulk detachment
167
public void detachFromAllTargets() {
168
for (Map.Entry<TargetID, SessionID> entry : activeSessions.entrySet()) {
169
try {
170
devTools.send(target.detachFromTarget(
171
Optional.of(entry.getValue()),
172
Optional.of(entry.getKey())
173
));
174
} catch (Exception e) {
175
System.err.println("Failed to detach from target: " + e.getMessage());
176
}
177
}
178
activeSessions.clear();
179
}
180
```
181
182
### Target Filtering and Selection
183
184
```java
185
// Filter targets by criteria
186
public List<TargetInfo> findTargetsByUrl(String urlPattern) {
187
List<TargetInfo> allTargets = devTools.send(target.getTargets());
188
189
return allTargets.stream()
190
.filter(info -> info.getUrl().matches(urlPattern))
191
.collect(Collectors.toList());
192
}
193
194
public Optional<TargetInfo> findTargetByTitle(String title) {
195
List<TargetInfo> allTargets = devTools.send(target.getTargets());
196
197
return allTargets.stream()
198
.filter(info -> title.equals(info.getTitle()))
199
.findFirst();
200
}
201
202
// Target type filtering
203
public List<TargetInfo> getPageTargets() {
204
List<TargetInfo> allTargets = devTools.send(target.getTargets());
205
206
return allTargets.stream()
207
.filter(info -> "page".equals(info.getType()))
208
.collect(Collectors.toList());
209
}
210
```
211
212
## Types
213
214
### Target Model Types
215
216
```java { .api }
217
// From idealized target model
218
import org.openqa.selenium.devtools.idealized.target.model.TargetID;
219
import org.openqa.selenium.devtools.idealized.target.model.SessionID;
220
import org.openqa.selenium.devtools.idealized.target.model.TargetInfo;
221
222
// From idealized browser model
223
import org.openqa.selenium.devtools.idealized.browser.model.BrowserContextID;
224
```
225
226
### Target Information Structure
227
228
```java { .api }
229
// TargetInfo contains comprehensive target details
230
class TargetInfo {
231
public TargetInfo(
232
TargetID targetId,
233
String type,
234
String title,
235
String url,
236
boolean attached,
237
Optional<TargetID> openerId,
238
Optional<BrowserContextID> browserContextId
239
);
240
241
public TargetID getTargetId(); // Unique target identifier
242
public String getType(); // Target type (page, worker, etc.)
243
public String getTitle(); // Target title/name
244
public String getUrl(); // Target URL
245
public boolean getAttached(); // Whether target is already attached
246
public Optional<TargetID> getOpenerId(); // ID of target that opened this one
247
public Optional<BrowserContextID> getBrowserContextId(); // Browser context
248
}
249
```
250
251
### Identifier Types
252
253
```java { .api }
254
// Target and Session identifiers
255
class TargetID {
256
public TargetID(String value);
257
public String toString();
258
}
259
260
class SessionID {
261
public SessionID(String value);
262
public String toString();
263
}
264
265
class BrowserContextID {
266
public BrowserContextID(String value);
267
public String toString();
268
}
269
```
270
271
### CDP Target Types
272
273
```java { .api }
274
// From v129.target.model package
275
import org.openqa.selenium.devtools.v129.target.model.TargetInfo as CdpTargetInfo;
276
import org.openqa.selenium.devtools.v129.target.model.TargetID as CdpTargetID;
277
import org.openqa.selenium.devtools.v129.target.model.SessionID as CdpSessionID;
278
279
// Target commands from v129.target package
280
import org.openqa.selenium.devtools.v129.target.Target;
281
```
282
283
### Command and Event Types
284
285
```java { .api }
286
import org.openqa.selenium.devtools.Command;
287
import org.openqa.selenium.devtools.Event;
288
import org.openqa.selenium.devtools.ConverterFunctions;
289
import java.util.Map;
290
import java.util.List;
291
import java.util.Optional;
292
```