-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
4712 lines (4197 loc) · 889 KB
/
index.html
File metadata and controls
4712 lines (4197 loc) · 889 KB
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Andrew's Blog</title>
<meta name="author" content="Andrew">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta property="og:site_name" content="Andrew's Blog"/>
<meta property="og:image" content=""/>
<link href="/favicon.png" rel="icon">
<link rel="stylesheet" href="/css/bootstrap.min.css" media="screen" type="text/css">
<link rel="stylesheet" href="/css/font-awesome.css" media="screen" type="text/css">
<link rel="stylesheet" href="/css/style.css" media="screen" type="text/css">
<link rel="stylesheet" href="/css/highlight.css" media="screen" type="text/css">
<link rel="stylesheet" href="/css/google-fonts.css" media="screen" type="text/css">
<link rel="stylesheet" href="/css/notebook.css" media="screen" type="text/css">
<link rel="stylesheet" href="/css/tabs.css" media="screen" type="text/css">
<!--[if lt IE 9]><script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/tabs.js"></script>
<!-- analytics -->
<!-- Load mathjax -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS_CHTML-full,Safe"></script>
<!-- MathJax configuration -->
<script type="text/x-mathjax-config">
init_mathjax = function() {
if (window.MathJax) {
// MathJax loaded
MathJax.Hub.Config({
TeX: {
equationNumbers: {
autoNumber: "AMS",
useLabelIds: true
}
},
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
processEscapes: true,
processEnvironments: true
},
displayAlign: 'center',
CommonHTML: {
linebreaks: {
automatic: true
}
}
});
MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
}
}
init_mathjax();
</script>
<!-- End of mathjax configuration -->
<meta name="generator" content="Hexo 6.3.0"></head>
<body>
<nav id="main-nav" class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<button type="button" class="navbar-header navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Andrew's Blog</a>
<div class="collapse navbar-collapse nav-menu">
<ul class="nav navbar-nav">
<li>
<a href="/archives" title="All the articles.">
<i class="fa fa-archive"></i>Archives
</a>
</li>
<li>
<a href="/tags" title="All the tags.">
<i class="fa fa-tags"></i>Tags
</a>
</li>
<li>
<a href="/about" title="About me.">
<i class="fa fa-user"></i>About
</a>
</li>
</ul>
</div>
</div> <!-- container -->
</nav>
<div class="clearfix"></div>
<div class="container">
<div class="content">
<div class="page-header">
<h1>Andrew's Blog</h1>
</div>
<div class="row page">
<div class="col-md-9">
<div class="slogan">
<i class="fa fa-heart"></i>
PURSUE ULTIMATE FACTS
</div>
<div class="mypage">
<!-- title and entry -->
<!-- display as entry -->
<h3 class="title">
<div class="date"> 2026-02-07 </div>
<div class="article-title"><a href="/2026/02/07/Agent-Browser/" >从 Playwright 到 agent-browser:基于 Accessibility Tree 的浏览器自动化与 AI Agent 实践</a></div>
</h3>
<div class="entry">
<div class="row">
<div class="col-md-12">
<p>这篇文章整理了一次从『传统浏览器自动化测试』一路深入到『AI Agent 驱动浏览器操作』的完整思考路径,核心关键词包括 <strong>Playwright</strong>、<strong>Rust</strong>、<strong>playwright-rs</strong>、<strong>agent-browser</strong>、<strong>Accessibility Tree</strong>、<strong>LLM</strong>。如果你正在思考如何让浏览器自动化更稳、更智能,或者如何把 LLM 接进真实网页操作,这篇总结可以作为一份系统性参考。</p>
<hr>
<h2 id="一、浏览器自动化的三条路线"><a href="#一、浏览器自动化的三条路线" class="headerlink" title="一、浏览器自动化的三条路线"></a>一、浏览器自动化的三条路线</h2><p>在讨论具体工具前,先明确三种常见路线:</p>
<ol>
<li><p><strong>官方 Playwright(Node.js / Python / Java / .NET)</strong><br>工程能力最强,生态最成熟,适合严肃的 E2E 测试。</p>
</li>
<li><p><strong>Rust + Playwright</strong><br>通过 Rust bindings 调用 Playwright(如 <code>playwright-rs</code>),在 Rust 工程内完成浏览器自动化。</p>
</li>
<li><p><strong>Rust WebDriver / Selenium 生态</strong><br>标准化强,但对现代浏览器能力(自动等待、可访问性、trace)支持有限。</p>
</li>
</ol>
<p>本文重点集中在 <strong>路线 2</strong> 以及由此延伸出的 <strong>agent-browser + LLM</strong> 思路。</p>
<hr>
<h2 id="二、playwright-rs:在-Rust-中写浏览器自动化测试"><a href="#二、playwright-rs:在-Rust-中写浏览器自动化测试" class="headerlink" title="二、playwright-rs:在 Rust 中写浏览器自动化测试"></a>二、playwright-rs:在 Rust 中写浏览器自动化测试</h2><p><code>playwright-rs</code> 是目前最有前途的 Rust Playwright 绑定:</p>
<ul>
<li>基于 <strong>官方 Playwright Server(Node.js)</strong></li>
<li>Rust 侧通过 JSON-RPC 调用</li>
<li>支持 Chromium / Firefox / WebKit</li>
<li>支持 Linux / macOS / Windows</li>
<li>已在 GitHub Actions 跨平台跑通</li>
</ul>
<h3 id="一个最小的-Rust-测试用例"><a href="#一个最小的-Rust-测试用例" class="headerlink" title="一个最小的 Rust 测试用例"></a>一个最小的 Rust 测试用例</h3><figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#[tokio::test]</span></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">fn</span> <span class="title function_">smoke_test</span>() <span class="punctuation">-></span> <span class="type">Result</span><(), <span class="type">Box</span><<span class="keyword">dyn</span> std::error::Error>> {</span><br><span class="line"> <span class="keyword">let</span> <span class="variable">pw</span> = Playwright::<span class="title function_ invoke__">launch</span>().<span class="keyword">await</span>?;</span><br><span class="line"> <span class="keyword">let</span> <span class="variable">browser</span> = pw.<span class="title function_ invoke__">chromium</span>().<span class="title function_ invoke__">launch</span>().<span class="keyword">await</span>?;</span><br><span class="line"> <span class="keyword">let</span> <span class="variable">page</span> = browser.<span class="title function_ invoke__">new_page</span>().<span class="keyword">await</span>?;</span><br><span class="line"></span><br><span class="line"> page.<span class="title function_ invoke__">goto</span>(<span class="string">"https://example.com"</span>, <span class="literal">None</span>).<span class="keyword">await</span>?;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">let</span> <span class="variable">heading</span> = page.<span class="title function_ invoke__">locator</span>(<span class="string">"role=heading"</span>).<span class="keyword">await</span>;</span><br><span class="line"> <span class="built_in">assert!</span>(heading.<span class="title function_ invoke__">is_visible</span>().<span class="keyword">await</span>?);</span><br><span class="line"></span><br><span class="line"> browser.<span class="title function_ invoke__">close</span>().<span class="keyword">await</span>?;</span><br><span class="line"> <span class="title function_ invoke__">Ok</span>(())</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>关键点在于:推荐使用 role / name 等基于可访问性语义的 locator,而不是 CSS selector。</p>
<hr>
<h2 id="三、Accessibility-Tree-是什么?为什么它更稳?"><a href="#三、Accessibility-Tree-是什么?为什么它更稳?" class="headerlink" title="三、Accessibility Tree 是什么?为什么它更稳?"></a>三、Accessibility Tree 是什么?为什么它更稳?</h2><ol>
<li><p>它不是 DOM</p>
<p>Accessibility Tree 是浏览器内部为屏幕阅读器和辅助技术维护的一棵语义树:</p>
<ul>
<li>节点是 button / textbox / heading / link</li>
<li>而不是 div / span</li>
<li>包含 role、accessible name、state(disabled/checked/expanded)</li>
</ul>
<p>它由浏览器从 DOM + CSS + ARIA 自动计算生成。</p>
</li>
<li><p>为什么适合自动化与 AI ?</p>
<ul>
<li>更贴近『人类理解的页面结构』</li>
<li>对 class、布局、嵌套不敏感</li>
<li>天然支持『这个按钮叫什么』『这个输入框是干嘛的』</li>
</ul>
</li>
</ol>
<p>Playwright 原生支持基于 accessibility 的定位,这也是它明显强于 Selenium 的地方。</p>
<p>ARIA(Accessible Rich Internet Applications)是一套给 Web 元素补充『语义、名称和状态』的规范,<br>浏览器据此计算 Accessibility Tree,供辅助技术、测试工具和 AI 使用。<br>ARIA 不是 Accessibility Tree 的『开关』,而是『校准器』;没有 ARIA,浏览器也能计算出树,只是精度可能不同。</p>
<hr>
<h2 id="四、Selenium-为什么很难支持-Accessibility-Tree-?"><a href="#四、Selenium-为什么很难支持-Accessibility-Tree-?" class="headerlink" title="四、Selenium 为什么很难支持 Accessibility Tree ?"></a>四、Selenium 为什么很难支持 Accessibility Tree ?</h2><p>结论先行:<br>Selenium 短期内几乎不可能完整支持 accessibility tree。</p>
<br />
<p>原因包括:</p>
<ul>
<li>WebDriver 标准主要围绕 DOM</li>
<li>Accessibility Tree 属于浏览器内部语义模型</li>
<li>AOM(Accessibility Object Model)仍处于实验/讨论阶段</li>
<li>不同浏览器、不同操作系统的无障碍实现差异巨大</li>
</ul>
<p>因此,Selenium 通常只能:</p>
<ul>
<li>结合 axe-core 等工具做无障碍检测</li>
<li>而不是『读取并操作 accessibility tree』</li>
</ul>
<hr>
<h2 id="五、agent-browser:为-AI-Agent-设计的浏览器控制层"><a href="#五、agent-browser:为-AI-Agent-设计的浏览器控制层" class="headerlink" title="五、agent-browser:为 AI Agent 设计的浏览器控制层"></a>五、<a target="_blank" rel="noopener" href="https://github.com/vercel-labs/agent-browser">agent-browser</a>:为 AI Agent 设计的浏览器控制层</h2><ol>
<li>agent-browser 是什么<ul>
<li>Vercel Labs 出品</li>
<li>一个 Rust CLI + Playwright 后端 的工具</li>
<li>不是测试框架,而是『浏览器操作协议的 CLI 实现』</li>
</ul>
</li>
</ol>
<p>它的核心创新是:</p>
<p>snapshot + ref-id 的交互模型</p>
<hr>
<ol start="2">
<li>snapshot 的工作原理<ol>
<li>从 Playwright 读取页面的 accessibility tree</li>
<li>语义裁剪(去掉无关节点)</li>
<li>为每个节点分配稳定的引用 ID(如 @e2)</li>
<li>输出一份『当前页面可操作状态』</li>
</ol>
</li>
</ol>
<p>示例:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">@e1 heading "Log in to Miro"</span><br><span class="line">@e2 textbox "Email"</span><br><span class="line">@e3 button "Continue"</span><br></pre></td></tr></table></figure>
<hr>
<ol start="3">
<li>用 ref-id 操作页面</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">agent-browser fill @e2 <span class="string">"test@example.com"</span></span><br><span class="line">agent-browser click @e3</span><br></pre></td></tr></table></figure>
<p>完全不需要 CSS selector。</p>
<hr>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<hr />
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span><span class="nb">source</span><span class="w"> </span>~/.bashrc
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<hr />
<p>第一步:确认你的环境前提</p>
<p>agent-browser 的现实前提有三点,缺一不可。</p>
<p>第一,macOS 本身没有问题,Intel 或 Apple Silicon 都可以。
第二,需要 <strong>Node.js(用于 Playwright)</strong>。
第三,需要 <strong>Rust(用于 agent-browser CLI)</strong>。</p>
<p>你可以先快速确认一下:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span>node<span class="w"> </span>-v
npm<span class="w"> </span>-v
rustc<span class="w"> </span>--version
cargo<span class="w"> </span>--version
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>v20.19.4
10.8.2
rustc 1.58.1 (db9d1b20b 2022-01-20)
cargo 1.58.0 (f01b232bc 2022-01-19)
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>如果 Node 没装,建议直接用:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span>brew<span class="w"> </span>install<span class="w"> </span>node
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span>pnpm<span class="w"> </span>setup
pnpm<span class="w"> </span>config<span class="w"> </span>get<span class="w"> </span>global-dir
mkdir<span class="w"> </span>-p<span class="w"> </span>~/.pnpm-global
pnpm<span class="w"> </span>config<span class="w"> </span><span class="nb">set</span><span class="w"> </span>global-dir<span class="w"> </span>~/.pnpm-global
<span class="nb">source</span><span class="w"> </span>~/.zshrc
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>如果 Rust 没装:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span>curl<span class="w"> </span>--proto<span class="w"> </span><span class="s1">'=https'</span><span class="w"> </span>--tlsv1.2<span class="w"> </span>-sSf<span class="w"> </span>https://sh.rustup.rs<span class="w"> </span><span class="p">|</span><span class="w"> </span>sh
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span>rustup<span class="w"> </span>update<span class="w"> </span>stable
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>或者:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span>curl<span class="w"> </span>--proto<span class="w"> </span><span class="s1">'=https'</span><span class="w"> </span>--tlsv1.2<span class="w"> </span>-sSf<span class="w"> </span>https://sh.rustup.rs<span class="w"> </span><span class="p">|</span><span class="w"> </span>sh<span class="w"> </span>-s<span class="w"> </span>--<span class="w"> </span>-y<span class="w"> </span>--profile<span class="w"> </span>minimal
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span>rustup<span class="w"> </span>update<span class="w"> </span>stable
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>装完记得重新开一个 shell,确保 <code>cargo</code> 在 PATH 里。</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<hr />
<p>第二步:安装 Playwright(这是最容易被忽略的一步)</p>
<p>agent-browser <strong>不是直接控制浏览器</strong>,它底层仍然依赖 Playwright 的 browser binaries。</p>
<p>在一个干净目录里执行一次即可:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span>npx<span class="w"> </span>playwright<span class="w"> </span>install
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>或者:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span>npm<span class="w"> </span>install<span class="w"> </span>-g<span class="w"> </span>playwright@latest
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span>playwright<span class="w"> </span>--version
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>Version 1.58.2
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span>playwright<span class="w"> </span>install
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>这一步会下载 Chromium / Firefox / WebKit。
macOS 上第一次运行浏览器时,系统可能会弹出『安全性与隐私』提示,允许即可。</p>
<p>如果这一步没做,后面 agent-browser 会『能启动,但打不开页面』。</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<hr />
<p>第三步:安装 agent-browser CLI</p>
<p>现在开始真正的 agent-browser。</p>
<p>官方仓库是:</p>
<p><a target="_blank" rel="noopener" href="https://github.com/vercel-labs/agent-browser">https://github.com/vercel-labs/agent-browser</a></p>
<p>最直接、最稳妥的方式是 <strong>从源码安装</strong>:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span>git<span class="w"> </span>clone<span class="w"> </span>https://github.com/vercel-labs/agent-browser.git
<span class="nb">cd</span><span class="w"> </span>agent-browser
pnpm<span class="w"> </span>install
pnpm<span class="w"> </span>build
pnpm<span class="w"> </span>build:native<span class="w"> </span><span class="c1"># Requires Rust (https://rustup.rs)</span>
pnpm<span class="w"> </span>link<span class="w"> </span>--global<span class="w"> </span><span class="c1"># Makes agent-browser available globally</span>
agent-browser<span class="w"> </span>install
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>安装成功后,你应该能看到:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span>agent-browser<span class="w"> </span>--help
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>
agent-browser - fast browser automation CLI for AI agents
Usage: agent-browser <command> [args] [options]
Core Commands:
open <url> Navigate to URL
click <sel> Click element (or @ref)
dblclick <sel> Double-click element
type <sel> <text> Type into element
fill <sel> <text> Clear and fill
press <key> Press key (Enter, Tab, Control+a)
hover <sel> Hover element
focus <sel> Focus element
check <sel> Check checkbox
uncheck <sel> Uncheck checkbox
select <sel> <val...> Select dropdown option
drag <src> <dst> Drag and drop
upload <sel> <files...> Upload files
download <sel> <path> Download file by clicking element
scroll <dir> [px] Scroll (up/down/left/right)
scrollintoview <sel> Scroll element into view
wait <sel|ms> Wait for element or time
screenshot [path] Take screenshot
pdf <path> Save as PDF
snapshot Accessibility tree with refs (for AI)
eval <js> Run JavaScript
connect <port|url> Connect to browser via CDP
close Close browser
Navigation:
back Go back
forward Go forward
reload Reload page
Get Info: agent-browser get <what> [selector]
text, html, value, attr <name>, title, url, count, box, styles
Check State: agent-browser is <what> <selector>
visible, enabled, checked
Find Elements: agent-browser find <locator> <value> <action> [text]
role, text, label, placeholder, alt, title, testid, first, last, nth
Mouse: agent-browser mouse <action> [args]
move <x> <y>, down [btn], up [btn], wheel <dy> [dx]
Browser Settings: agent-browser set <setting> [value]
viewport <w> <h>, device <name>, geo <lat> <lng>
offline [on|off], headers <json>, credentials <user> <pass>
media [dark|light] [reduced-motion]
Network: agent-browser network <action>
route <url> [--abort|--body <json>]
unroute [url]
requests [--clear] [--filter <pattern>]
Storage:
cookies [get|set|clear] Manage cookies (set supports --url, --domain, --path, --httpOnly, --secure, --sameSite, --expires)
storage <local|session> Manage web storage
Tabs:
tab [new|list|close|<n>] Manage tabs
Debug:
trace start|stop [path] Record trace
record start <path> [url] Start video recording (WebM)
record stop Stop and save video
console [--clear] View console logs
errors [--clear] View page errors
highlight <sel> Highlight element
Sessions:
session Show current session name
session list List active sessions
Setup:
install Install browser binaries
install --with-deps Also install system dependencies (Linux)
Snapshot Options:
-i, --interactive Only interactive elements
-c, --compact Remove empty structural elements
-d, --depth <n> Limit tree depth
-s, --selector <sel> Scope to CSS selector
Options:
--session <name> Isolated session (or AGENT_BROWSER_SESSION env)
--profile <path> Persistent browser profile (or AGENT_BROWSER_PROFILE env)
--state <path> Load storage state from JSON file (or AGENT_BROWSER_STATE env)
--headers <json> HTTP headers scoped to URL's origin (for auth)
--executable-path <path> Custom browser executable (or AGENT_BROWSER_EXECUTABLE_PATH)
--extension <path> Load browser extensions (repeatable)
--args <args> Browser launch args, comma or newline separated (or AGENT_BROWSER_ARGS)
e.g., --args "--no-sandbox,--disable-blink-features=AutomationControlled"
--user-agent <ua> Custom User-Agent (or AGENT_BROWSER_USER_AGENT)
--proxy <server> Proxy server URL (or AGENT_BROWSER_PROXY)
e.g., --proxy "http://user:pass@127.0.0.1:7890"
--proxy-bypass <hosts> Bypass proxy for these hosts (or AGENT_BROWSER_PROXY_BYPASS)
e.g., --proxy-bypass "localhost,*.internal.com"
--ignore-https-errors Ignore HTTPS certificate errors
--allow-file-access Allow file:// URLs to access local files (Chromium only)
-p, --provider <name> Browser provider: ios, browserbase, kernel, browseruse
--device <name> iOS device name (e.g., "iPhone 15 Pro")
--json JSON output
--full, -f Full page screenshot
--headed Show browser window (not headless)
--cdp <port> Connect via CDP (Chrome DevTools Protocol)
--debug Debug output
--version, -V Show version
Environment:
AGENT_BROWSER_SESSION Session name (default: "default")
AGENT_BROWSER_EXECUTABLE_PATH Custom browser executable path
AGENT_BROWSER_PROVIDER Browser provider (ios, browserbase, kernel, browseruse)
AGENT_BROWSER_STREAM_PORT Enable WebSocket streaming on port (e.g., 9223)
AGENT_BROWSER_IOS_DEVICE Default iOS device name
AGENT_BROWSER_IOS_UDID Default iOS device UDID
Examples:
agent-browser open example.com
agent-browser snapshot -i # Interactive elements only
agent-browser click @e2 # Click by ref from snapshot
agent-browser fill @e3 "test@example.com"
agent-browser find role button click --name Submit
agent-browser get text @e1
agent-browser screenshot --full
agent-browser --cdp 9222 snapshot # Connect via CDP port
agent-browser --profile ~/.myapp open example.com # Persistent profile
iOS Simulator (requires Xcode and Appium):
agent-browser -p ios open example.com # Use default iPhone
agent-browser -p ios --device "iPhone 15 Pro" open url # Specific device
agent-browser -p ios device list # List simulators
agent-browser -p ios swipe up # Swipe gesture
agent-browser -p ios tap @e1 # Touch element
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>如果提示 command not found,检查 <code>$HOME/.cargo/bin</code> 是否在 PATH 中。</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<hr />
<p>第四步:做一次最小可行测试(不接 LLM)</p>
<p>不要一上来就连 LLM,先验证三件事:</p>
<ol>
<li>Playwright 能起浏览器</li>
<li>agent-browser 能读 accessibility tree</li>
<li>snapshot 能正常输出</li>
</ol>
<p>按顺序来:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span>agent-browser<span class="w"> </span>open<span class="w"> </span>https://miro.com/login/<span class="w"> </span>--headed
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre><span class="ansi-green-fg">✓</span> <span class="ansi-bold">Sign in | Miro | The Visual Workspace for Innovation</span>
https://miro.com/login/
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>你应该能看到浏览器窗口被打开。</p>
<p>接着:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span>agent-browser<span class="w"> </span>snapshot
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>- document:
- button "Skip to main content" [ref=e1]
- banner:
- link "Go to Miro main page" [ref=e2]:
- /url: /index/
- text: Miro Logo
- button "Language" [ref=e3]: en
- text: "Current language is: English"
- link "Sign up" [ref=e4]:
- /url: https://miro.com/signup/
- main:
- heading "Sign in to Miro" [ref=e5] [level=1]
- group "Sign-in options":
- button "SSO" [ref=e6]
- button "Google" [ref=e7]
- button "Microsoft 365" [ref=e8]
- button "Show more sign-in options" [ref=e9]
- text: Email
- textbox "Email" [ref=e10]:
- /placeholder: Enter your email address
- text: Password
- textbox "Password" [ref=e11]:
- /placeholder: Enter your password
- link "Forgot password?" [ref=e12]:
- /url: https://miro.com/recover/
- checkbox "Remember me" [ref=e13] [checked]
- paragraph:
- paragraph: Remember me
- button "Continue with email" [ref=e14]
- button "Sign in to a different region or a private organization" [ref=e15]
- paragraph:
- text: Trouble signing in?
- link "Request a magic sign in link" [ref=e16]:
- /url: /login/passwordless/
- text: or
- link "reset your password" [ref=e17]:
- /url: /recover/
- text: .
- alert
- iframe
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>正常情况下,会输出类似:</p>
<pre><code>@e1 heading "Example Domain"
@e2 paragraph "This domain is for use in illustrative examples..."
@e3 link "More information..."
</code></pre>
<p>只要这一步成功,说明你的 <strong>macOS + Playwright + agent-browser 环境是健康的</strong>。</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<hr />
<p>第五步:实际操作页面(验证 ref-id 模型)</p>
<p>继续在 example.com 上试:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span>agent-browser<span class="w"> </span>click<span class="w"> </span>@e22
agent-browser<span class="w"> </span>snapshot
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>你会看到页面发生变化,ref-id 全部重新分配。
这一步非常关键,它验证了你对 agent-browser 核心原则的理解:</p>
<blockquote>
<p>ref-id 只在当前 snapshot 有意义</p>
</blockquote>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span>agent-browser<span class="w"> </span>click<span class="w"> </span>@e4
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span>agent-browser<span class="w"> </span>fill<span class="w"> </span>@e6<span class="w"> </span><span class="s2">"andy@dtype.info"</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<hr />
<p>第六步:测试一个真实复杂页面(推荐)</p>
<p>不要立刻上 Miro、Google 这种登录页,先选一个『复杂但不反爬』的页面,比如:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span>agent-browser<span class="w"> </span>open<span class="w"> </span>https://news.ycombinator.com
agent-browser<span class="w"> </span>snapshot
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>你会看到大量:</p>
<pre><code>@e1 link "Hacker News"
@e2 link "new"
@e3 link "past"
...
</code></pre>
<p>试着:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-bash"><pre><span></span>agent-browser<span class="w"> </span>click<span class="w"> </span>@e2
agent-browser<span class="w"> </span>snapshot
</pre></div>
</div>
</div>
</div>
</div>