docker运行的playwright记录
下载官方镜像:https://playwright.dev/python/docs/docker
使用命令;
docker pull mcr.microsoft.com/playwright/python:v1.31.0-focal
然后运行:
docker run -it --rm --name=test_play -v /Users/kaka/miniconda3/envs/playwright_tianmao/project/src:/src mcr.microsoft.com/playwright/python:v1.31.0-focal /bin/bash
因为把本地的那个工作目录就挂载到容器内了么,所以本地修改代码,容器内的也可以运行:
1 root@5958fc57326e:/# python /src/tian_mao.py
2 Traceback (most recent call last):
3 File "/src/tian_mao.py", line 100, in <module>
4 run (playwright)
5 File "/src/tian_mao.py", line 22, in run
6 context = playwright.firefox.launch_persistent_context (
7 File "/usr/local/lib/python3.8/dist-packages/playwright/sync_api/_generated.py", line 14691, in launch_persistent_context
8 self._sync(
9 File "/usr/local/lib/python3.8/dist-packages/playwright/_impl/_sync_base.py", line 104, in _sync
10 return task.result()
11 File "/usr/local/lib/python3.8/dist-packages/playwright/_impl/_browser_type.py", line 155, in launch_persistent_context
12 from_channel(await self._channel.send("launchPersistentContext", params)),
13 File "/usr/local/lib/python3.8/dist-packages/playwright/_impl/_connection.py", line 44, in send
14 return await self._connection.wrap_api_call(
15 File "/usr/local/lib/python3.8/dist-packages/playwright/_impl/_connection.py", line 419, in wrap_api_call
16 return await cb()
17 File "/usr/local/lib/python3.8/dist-packages/playwright/_impl/_connection.py", line 79, in inner_send
18 result = next(iter(done)).result()
19 playwright._impl._api_types.Error:
20 ╔════════════════════════════════════════════════════════════════════════════════════════════════╗
21 ║ Looks like you launched a headed browser without having a XServer running. ║
22 ║ Set either 'headless: true' or use 'xvfb-run <your-playwright-app>' before running Playwright. ║
23 ║ ║
24 ║ <3 Playwright Team ║
25 ╚════════════════════════════════════════════════════════════════════════════════════════════════╝
26 =========================== logs ===========================
27 <launching> /ms-playwright/firefox-1378/firefox/firefox -no-remote -wait-for-browser -foreground -profile /Users/kaka/firefox2 -juggler-pipe --disable-blink-features=AutomationControlled --disable-extensions --disable-popup-blocking --ignore-certificate-errors --disable-plugins-discovery --no-first-run --no-service-autorun --no-default-browser-check --disable-dev-shm-usage about:blank
28 <launched> pid=232
29 [pid=232][err] Error: no DISPLAY environment variable specified
30 [pid=232] <process did exit: exitCode=1, signal=null>
31 [pid=232] starting temporary directories cleanup
32 ============================================================
33 root@5958fc57326e:/#
这个也报错,但是和之前的我的运行chrome的相比,这个起码还可以运行:
所以我在这里面,我就用的修改成了firefox,但是因为我的设置为headless=False所以,我们先测试一下,这个如果改为TRUE的话能不能跑:
1 root@5958fc57326e:/# python /src/tian_mao.py 2 快速进入->Exception:Timeout 2000ms exceeded. 3 =========================== logs =========================== 4 waiting for get_by_role("button", name="快速进入") 5 ============================================================ 6 未获取到登录的淘宝昵称->Exception:Timeout 5000ms exceeded. 7 =========================== logs =========================== 8 waiting for locator(".site-nav-bd > ul.site-nav-bd-l > li#J_SiteNavLogin > div.site-nav-menu-hd > div.site-nav-user > a.site-nav-login-info-nick") 9 ============================================================ 10 root@5958fc57326e:/#
虽然还是有问题,但是这个问题是运行的程序的问题,起码证明这个官方的这个可以用,我在试一下这个如果改为False之后,按照上文的报错的指引,如此运行:
1 root@5958fc57326e:/# xvfb-run python src/tian_mao.py 2 获取拖动按钮位置并拖动->Exception:Timeout 2000ms exceeded. 3 =========================== logs =========================== 4 waiting for frame_locator("#baxia-dialog-content").locator("#nc_1_n1z") 5 ============================================================ 6 快速进入->Exception:Timeout 2000ms exceeded. 7 =========================== logs =========================== 8 waiting for get_by_role("button", name="快速进入") 9 ============================================================ 10 获取到登录的淘宝昵称是:tb4210828_2012 11 root@5958fc57326e:/#
可以看到,获取到了我的淘宝昵称。