rust_drission 0.2.1

Browser automation library for Rust via Chrome DevTools Protocol (CDP). Connect or launch Chrome, control pages/elements, run JS, cookies, screenshots, request/response listening. API inspired by DrissionPage.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! CDP DOM 与 Runtime 调用封装

use crate::cdp::{CdpClient, CdpError};
use serde_json::{json, Value};

fn is_transient_node_error(e: &CdpError) -> bool {
    match e {
        CdpError::Protocol { message, .. } => {
            message.contains("No node with given id")
                || message.contains("Could not find node with given id")
                || message.contains("Could not find object with given id")
                || message.contains("Object has been collected")
        }
        _ => false,
    }
}

/// 启用 DOM 与 Runtime 并返回文档根 nodeId
pub fn get_document_root(
    client: &CdpClient,
    session_id: &str,
) -> Result<i64, CdpError> {
    client.send_with_session("DOM.enable", None, Some(session_id))?;
    client.send_with_session("Runtime.enable", None, Some(session_id))?;
    let result = client.send_with_session("DOM.getDocument", None, Some(session_id))?;
    let root = result
        .get("root")
        .and_then(|v| v.get("nodeId"))
        .and_then(Value::as_i64)
        .ok_or_else(|| CdpError::Protocol {
            id: None,
            code: -1,
            message: "DOM.getDocument did not return root.nodeId".into(),
        })?;
    Ok(root)
}

/// 在指定节点下用 CSS 选择器查单个节点,返回 nodeId
pub fn query_selector(
    client: &CdpClient,
    session_id: &str,
    node_id: i64,
    selector: &str,
) -> Result<Option<i64>, CdpError> {
    let params = json!({ "nodeId": node_id, "selector": selector });
    let result = client.send_with_session("DOM.querySelector", Some(params), Some(session_id))?;
    let node_id = result.get("nodeId").and_then(Value::as_i64);
    Ok(node_id.filter(|&id| id != 0))
}

/// 在指定节点下用 CSS 选择器查多个节点,返回 nodeId 列表(通过 evaluate + requestNode)
pub fn query_selector_all(
    client: &CdpClient,
    session_id: &str,
    selector: &str,
) -> Result<Vec<i64>, CdpError> {
    // 在页面中执行 document.querySelectorAll(selector),返回 NodeList 的 objectId
    let escaped = serde_json::to_string(selector).map_err(CdpError::Json)?;
    let expr = format!("document.querySelectorAll({})", escaped);
    let params = json!({
        "expression": expr,
        "returnByValue": false
    });
    let result = client.send_with_session("Runtime.evaluate", Some(params), Some(session_id))?;
    let obj = result.get("result").ok_or_else(|| CdpError::Protocol {
        id: None,
        code: -1,
        message: "Runtime.evaluate did not return a result payload".into(),
    })?;
    let object_id = obj.get("objectId").and_then(Value::as_str);
    let Some(object_id) = object_id else {
        return Ok(Vec::new());
    };
    // Array.from(this) 得到元素数组
    let params = json!({
        "functionDeclaration": "function(){ return Array.from(this); }",
        "objectId": object_id
    });
    let result = client.send_with_session("Runtime.callFunctionOn", Some(params), Some(session_id))?;
    let result_val = result.get("result").ok_or_else(|| CdpError::Protocol {
        id: None,
        code: -1,
        message: "Runtime.callFunctionOn did not return a result payload".into(),
    })?;
    let list_obj_id = result_val.get("objectId").and_then(Value::as_str);
    let Some(list_obj_id) = list_obj_id else {
        return Ok(Vec::new());
    };
    // 取 length
    let params = json!({
        "functionDeclaration": "function(){ return this.length; }",
        "objectId": list_obj_id
    });
    let result = client.send_with_session("Runtime.callFunctionOn", Some(params), Some(session_id))?;
    let len = result
        .get("result")
        .and_then(|r| r.get("value"))
        .and_then(Value::as_u64)
        .unwrap_or(0) as usize;
    let mut node_ids = Vec::with_capacity(len);
    for i in 0..len {
        let params = json!({
            "functionDeclaration": "function(i){ return this[i]; }",
            "objectId": list_obj_id,
            "arguments": [{"value": i}]
        });
        let result = match client.send_with_session("Runtime.callFunctionOn", Some(params), Some(session_id)) {
            Ok(v) => v,
            Err(e) if is_transient_node_error(&e) => continue,
            Err(e) => return Err(e),
        };
        let elem = result.get("result").and_then(|r| r.get("objectId")).and_then(Value::as_str);
        if let Some(oid) = elem {
            let params = json!({ "objectId": oid });
            let res = match client.send_with_session("DOM.requestNode", Some(params), Some(session_id)) {
                Ok(v) => v,
                Err(e) if is_transient_node_error(&e) => continue,
                Err(e) => return Err(e),
            };
            if let Some(nid) = res.get("nodeId").and_then(Value::as_i64) {
                node_ids.push(nid);
            }
        }
    }
    Ok(node_ids)
}

/// 获取 iframe/frame 元素的 contentDocument 的 nodeId(仅同源可用,跨域返回 None)。
pub fn get_iframe_content_document_node_id(
    client: &CdpClient,
    session_id: &str,
    iframe_node_id: i64,
) -> Result<Option<i64>, CdpError> {
    let object_id = resolve_node_to_object_id(client, session_id, iframe_node_id)?;
    let params = json!({
        "functionDeclaration": "function(){ return this.contentDocument; }",
        "objectId": object_id
    });
    let result = client.send_with_session("Runtime.callFunctionOn", Some(params), Some(session_id))?;
    let result_val = result.get("result").ok_or_else(|| CdpError::Protocol {
        id: None,
        code: -1,
        message: "Runtime.callFunctionOn did not return a result payload".into(),
    })?;
    // 跨域 iframe 时 contentDocument 为 null,subtype 为 "null"
    let obj_id = result_val.get("objectId").and_then(Value::as_str);
    let Some(obj_id) = obj_id else {
        return Ok(None);
    };
    let params = json!({ "objectId": obj_id });
    let res = client.send_with_session("DOM.requestNode", Some(params), Some(session_id))?;
    let node_id = res.get("nodeId").and_then(Value::as_i64).filter(|&id| id != 0);
    Ok(node_id)
}

/// 在指定文档根节点下执行 querySelectorAll,返回 nodeId 列表(用于 iframe 内查找)。
pub fn query_selector_all_under_root(
    client: &CdpClient,
    session_id: &str,
    root_node_id: i64,
    selector: &str,
) -> Result<Vec<i64>, CdpError> {
    let object_id = resolve_node_to_object_id(client, session_id, root_node_id)?;
    let params = json!({
        "functionDeclaration": format!("function(sel){{ return Array.from(this.querySelectorAll(sel)); }}"),
        "objectId": object_id,
        "arguments": [{"value": selector}]
    });
    let result = client.send_with_session("Runtime.callFunctionOn", Some(params), Some(session_id))?;
    let result_val = result.get("result").ok_or_else(|| CdpError::Protocol {
        id: None,
        code: -1,
        message: "Runtime.callFunctionOn did not return a result payload".into(),
    })?;
    let list_obj_id = result_val.get("objectId").and_then(Value::as_str);
    let Some(list_obj_id) = list_obj_id else {
        return Ok(Vec::new());
    };
    let params = json!({
        "functionDeclaration": "function(){ return this.length; }",
        "objectId": list_obj_id
    });
    let result = client.send_with_session("Runtime.callFunctionOn", Some(params), Some(session_id))?;
    let len = result
        .get("result")
        .and_then(|r| r.get("value"))
        .and_then(Value::as_u64)
        .unwrap_or(0) as usize;
    let mut node_ids = Vec::with_capacity(len);
    for i in 0..len {
        let params = json!({
            "functionDeclaration": "function(i){ return this[i]; }",
            "objectId": list_obj_id,
            "arguments": [{"value": i}]
        });
        let result = match client.send_with_session("Runtime.callFunctionOn", Some(params), Some(session_id)) {
            Ok(v) => v,
            Err(e) if is_transient_node_error(&e) => continue,
            Err(e) => return Err(e),
        };
        let elem = result.get("result").and_then(|r| r.get("objectId")).and_then(Value::as_str);
        if let Some(oid) = elem {
            let params = json!({ "objectId": oid });
            let res = match client.send_with_session("DOM.requestNode", Some(params), Some(session_id)) {
                Ok(v) => v,
                Err(e) if is_transient_node_error(&e) => continue,
                Err(e) => return Err(e),
            };
            if let Some(nid) = res.get("nodeId").and_then(Value::as_i64) {
                node_ids.push(nid);
            }
        }
    }
    Ok(node_ids)
}

/// 在主文档及所有同源 iframe 的 document 中执行 querySelectorAll,返回 (nodeId, objectId) 列表。
/// objectId 为取 nodeId 时持有的引用,iframe 内节点优先用其做 callFunctionOn,避免 nodeId 失效。
pub fn query_selector_all_including_same_origin_frames(
    client: &CdpClient,
    session_id: &str,
    selector: &str,
) -> Result<Vec<(i64, String)>, CdpError> {
    client.send_with_session("DOM.enable", None, Some(session_id))?;
    client.send_with_session("Runtime.enable", None, Some(session_id))?;
    let escaped = serde_json::to_string(selector).map_err(CdpError::Json)?;
    // 递归收集 document + 所有同源 iframe 的 contentDocument 中匹配元素
    let expr = format!(
        r#"(function(selector){{
            var results = [];
            function collect(root){{
                if(!root || !root.querySelectorAll) return;
                try{{
                    var list = root.querySelectorAll(selector);
                    for(var i=0;i<list.length;i++) results.push(list[i]);
                    var frames = root.querySelectorAll('iframe,frame');
                    for(var j=0;j<frames.length;j++){{
                        try{{
                            var doc = frames[j].contentDocument;
                            if(doc) collect(doc);
                        }}catch(e){{}}
                    }}
                }}catch(e){{}}
            }}
            collect(document);
            return results;
        }})({})"#,
        escaped
    );
    let params = json!({
        "expression": expr,
        "returnByValue": false
    });
    let result = client.send_with_session("Runtime.evaluate", Some(params), Some(session_id))?;
    let obj = result.get("result").ok_or_else(|| CdpError::Protocol {
        id: None,
        code: -1,
        message: "Runtime.evaluate did not return a result payload".into(),
    })?;
    let list_obj_id = obj.get("objectId").and_then(Value::as_str);
    let Some(list_obj_id) = list_obj_id else {
        return Ok(Vec::new());
    };
    let params = json!({
        "functionDeclaration": "function(){ return this.length; }",
        "objectId": list_obj_id
    });
    let result = client.send_with_session("Runtime.callFunctionOn", Some(params), Some(session_id))?;
    let len = result
        .get("result")
        .and_then(|r| r.get("value"))
        .and_then(Value::as_u64)
        .unwrap_or(0) as usize;
    let mut pairs = Vec::with_capacity(len);
    for i in 0..len {
        let params = json!({
            "functionDeclaration": "function(i){ return this[i]; }",
            "objectId": list_obj_id,
            "arguments": [{"value": i}]
        });
        let result = match client.send_with_session("Runtime.callFunctionOn", Some(params), Some(session_id)) {
            Ok(v) => v,
            Err(e) if is_transient_node_error(&e) => continue,
            Err(e) => return Err(e),
        };
        let elem = result.get("result").and_then(|r| r.get("objectId")).and_then(Value::as_str);
        if let Some(oid) = elem {
            let params = json!({ "objectId": oid });
            let res = match client.send_with_session("DOM.requestNode", Some(params), Some(session_id)) {
                Ok(v) => v,
                Err(e) if is_transient_node_error(&e) => continue,
                Err(e) => return Err(e),
            };
            if let Some(nid) = res.get("nodeId").and_then(Value::as_i64) {
                pairs.push((nid, oid.to_string()));
            }
        }
    }
    Ok(pairs)
}

/// 获取节点 HTML
pub fn get_outer_html(
    client: &CdpClient,
    session_id: &str,
    node_id: i64,
) -> Result<String, CdpError> {
    let params = json!({ "nodeId": node_id });
    let result = client.send_with_session("DOM.getOuterHTML", Some(params), Some(session_id))?;
    result
        .get("outerHTML")
        .and_then(Value::as_str)
        .map(String::from)
        .ok_or_else(|| CdpError::Protocol {
            id: None,
            code: -1,
            message: "DOM.getOuterHTML did not return outerHTML".into(),
        })
}

/// 在整棵 DOM 树(含 iframe、shadow DOM)中搜索,返回 (searchId, resultCount)。
/// query 可为 CSS 选择器或 XPath 表达式(与 DevTools 元素搜索一致)。
pub fn perform_search(
    client: &CdpClient,
    session_id: &str,
    query: &str,
    include_user_agent_shadow_dom: bool,
) -> Result<(String, u32), CdpError> {
    client.send_with_session("DOM.enable", None, Some(session_id))?;
    let params = json!({
        "query": query,
        "includeUserAgentShadowDOM": include_user_agent_shadow_dom
    });
    let result = client.send_with_session("DOM.performSearch", Some(params), Some(session_id))?;
    let search_id = result
        .get("searchId")
        .and_then(Value::as_str)
        .ok_or_else(|| CdpError::Protocol {
            id: None,
            code: -1,
            message: "DOM.performSearch did not return searchId".into(),
        })?
        .to_string();
    let result_count = result
        .get("resultCount")
        .and_then(Value::as_u64)
        .unwrap_or(0) as u32;
    Ok((search_id, result_count))
}

/// 从 performSearch 的会话中取回 nodeId 列表
pub fn get_search_results(
    client: &CdpClient,
    session_id: &str,
    search_id: &str,
    from_index: u32,
    to_index: u32,
) -> Result<Vec<i64>, CdpError> {
    if from_index >= to_index {
        return Ok(Vec::new());
    }
    let params = json!({
        "searchId": search_id,
        "fromIndex": from_index,
        "toIndex": to_index
    });
    let result = client.send_with_session("DOM.getSearchResults", Some(params), Some(session_id))?;
    let node_ids = result
        .get("nodeIds")
        .and_then(Value::as_array)
        .map(|arr| {
            arr.iter()
                .filter_map(|v| v.as_i64())
                .filter(|&id| id != 0)
                .collect()
        })
        .unwrap_or_default();
    Ok(node_ids)
}

/// 丢弃搜索会话,释放资源
pub fn discard_search_results(
    client: &CdpClient,
    session_id: &str,
    search_id: &str,
) -> Result<(), CdpError> {
    let params = json!({ "searchId": search_id });
    client.send_with_session("DOM.discardSearchResults", Some(params), Some(session_id))?;
    Ok(())
}

/// nodeId -> objectId(用于 callFunctionOn)
pub fn resolve_node_to_object_id(
    client: &CdpClient,
    session_id: &str,
    node_id: i64,
) -> Result<String, CdpError> {
    let params = json!({ "nodeId": node_id });
    let result = client.send_with_session("DOM.resolveNode", Some(params), Some(session_id))?;
    result
        .get("object")
        .and_then(|o| o.get("objectId"))
        .and_then(Value::as_str)
        .map(String::from)
        .ok_or_else(|| CdpError::Protocol {
            id: None,
            code: -1,
            message: "DOM.resolveNode did not return object.objectId".into(),
        })
}

/// 由 nodeId 取 backendNodeId(与 DrissionPage 一致,用于元素稳定引用,nodeId 失效时可用 backend 刷新)
pub fn get_backend_node_id(
    client: &CdpClient,
    session_id: &str,
    node_id: i64,
) -> Result<i64, CdpError> {
    client.send_with_session("DOM.enable", None, Some(session_id))?;
    let params = json!({ "nodeId": node_id });
    let result = client.send_with_session("DOM.describeNode", Some(params), Some(session_id))?;
    result
        .get("node")
        .and_then(|n| n.get("backendNodeId"))
        .and_then(Value::as_i64)
        .ok_or_else(|| CdpError::Protocol {
            id: None,
            code: -1,
            message: "DOM.describeNode did not return node.backendNodeId".into(),
        })
}

/// 由 backendNodeId 直接取 objectId(与 Python _get_obj_id(backend_id) 一致,nodeId 失效时用)
pub fn resolve_backend_to_object_id(
    client: &CdpClient,
    session_id: &str,
    backend_node_id: i64,
) -> Result<String, CdpError> {
    let params = json!({ "backendNodeId": backend_node_id });
    let result = client.send_with_session("DOM.resolveNode", Some(params), Some(session_id))?;
    result
        .get("object")
        .and_then(|o| o.get("objectId"))
        .and_then(Value::as_str)
        .map(String::from)
        .ok_or_else(|| CdpError::Protocol {
            id: None,
            code: -1,
            message: "DOM.resolveNode(backendNodeId) did not return object.objectId".into(),
        })
}

/// 由 backendNodeId 取当前 nodeId(DOM 更新后 nodeId 会变,用 backend 可重新解析)
pub fn get_node_id_from_backend(
    client: &CdpClient,
    session_id: &str,
    backend_node_id: i64,
) -> Result<i64, CdpError> {
    client.send_with_session("DOM.enable", None, Some(session_id))?;
    let params = json!({ "backendNodeId": backend_node_id });
    let result = client.send_with_session("DOM.describeNode", Some(params), Some(session_id))?;
    result
        .get("node")
        .and_then(|n| n.get("nodeId"))
        .and_then(Value::as_i64)
        .ok_or_else(|| CdpError::Protocol {
            id: None,
            code: -1,
            message: "DOM.describeNode did not return node.nodeId".into(),
        })
}