File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ playwright >= 1.41.0
2+ requests >= 2.31.0
Original file line number Diff line number Diff line change 1+ from playwright .sync_api import sync_playwright
2+ import requests
3+
4+ def main ():
5+ # 通过API启动环境
6+ try :
7+ response = requests .post (
8+ 'http://localhost:9000/api/launchBrowser' ,
9+ json = {'id' : 1 },
10+ headers = {'Content-Type' : 'application/json' }
11+ ).json ()
12+ except Exception as err :
13+ print (f"错误: { err } " )
14+ return
15+
16+ # 返回debuggingPort
17+ print (response )
18+
19+ if not response .get ('success' ):
20+ return
21+
22+ # 使用playwright连接浏览器
23+ with sync_playwright () as p :
24+ browser = p .chromium .connect_over_cdp (f"http://localhost:{ response ['data' ]['debuggingPort' ]} " )
25+
26+ # 使用当前窗口
27+ context = browser .contexts [0 ]
28+ page = context .new_page ()
29+
30+ # 访问应用程序
31+ page .goto ('https://www.baidu.com/' )
32+
33+ page2 = context .new_page ()
34+ page2 .goto ('https://www.163.com/' )
35+
36+ # 其他测试代码...
37+
38+ # 关闭浏览器
39+ # browser.close() # 调试时可以注释掉此行
40+
41+ if __name__ == "__main__" :
42+ main ()
You can’t perform that action at this time.
0 commit comments