liuyuqi-cnb 2 months ago
parent
commit
3c41cb131e
6 changed files with 177 additions and 0 deletions
  1. 38 0
      Dockerfile
  2. 69 0
      README.md
  3. 11 0
      docker-compose.yml
  4. 21 0
      entrypoint.sh
  5. 17 0
      test/t1.py
  6. 21 0
      test/t2.py

+ 38 - 0
Dockerfile

@@ -0,0 +1,38 @@
+FROM mcr.microsoft.com/playwright:v1.37.0-jammy
+#  mcr.microsoft.com/playwright/python:v1.52.0
+
+# 设置非交互式环境变量和时区
+ENV DEBIAN_FRONTEND=noninteractive
+ENV TZ=Etc/UTC
+WORKDIR /app
+# 安装 Python 3.9 和 pip(修复时区阻塞问题)
+RUN  sed -i 's|http://.*archive.ubuntu.com|https://mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list && \
+     sed -i 's|http://.*security.ubuntu.com|https://mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list && \
+     apt-get update && \
+     apt-get install -y software-properties-common tzdata python3  python3-pip  ttf-wqy-microhei && \
+     ln -fs /usr/share/zoneinfo/$TZ /etc/localtime && \
+     dpkg-reconfigure --frontend noninteractive tzdata && \
+     add-apt-repository -y ppa:deadsnakes/ppa && \
+     pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple && \
+     pip3 config set global.trusted-host pypi.tuna.tsinghua.edu.cn
+
+# 安装 Playwright Python 库和浏览器驱动
+RUN python3 -m pip install playwright && \
+    python3 -m playwright install
+
+RUN pip install aiohttp toml pydantic fastapi sqlalchemy python-multipart uvicorn
+
+# 安装 Xvfb 和窗口管理器(支持有头模式)
+RUN apt-get install -y xvfb fluxbox && \
+    rm -rf /var/lib/apt/lists/*
+
+# 设置环境变量
+ENV DISPLAY=:99
+
+COPY ./app /app
+
+# 启动脚本
+COPY entrypoint.sh /entrypoint.sh
+RUN chmod +x /entrypoint.sh
+
+ENTRYPOINT ["/entrypoint.sh"]

+ 69 - 0
README.md

@@ -7,6 +7,7 @@
 使用超级简单,代码运行后会自动下载最新版chrome,edge,firefox,ffmpeg ,以及driver,600MB,首次运行需要下载环境。
 ```
 
+
 ```
 
 windows保存到 :
@@ -18,6 +19,7 @@ C:\Users\xx\AppData\Local\ms-playwright
 ```
 npm init playwright@latest new-project
 
+#pip download playwright -d ./down
 pip install playwright==1.33.0
 playwright install
 playwright install-deps
@@ -30,9 +32,76 @@ from playwright.sync_api import sync_playwright
 
 ```
 
+代码生成:
+```
+playwright codegen https://playwright.dev
+
+```
+访问 xx 链接的所有点击,表单等操作都会生成代码,类似于脚本录制。
+
 需要更多自定义,采用 selenium
 
 
+
+## MCP server
+
+可以通过python 或者 nodejs 安装 playwright ,
+
+```
+npm install -g @playwright/mcp@latest
+npm install -g @playwright@latest
+npx playwright install --with-deps
+```
+
+然后再 vscode, cherry-studio,Claude ,Cursor、Windsurf、Cline 等安装 mcp server:
+
+```
+
+{
+  "mcpServers": {
+    "playwright": {
+      "command": "cmd /c npx",       // windows
+      "command": "npx",
+      "args": [
+        "@playwright/mcp@latest"
+      ]
+    }
+  }
+}
+
+
+# 或者
+
+{
+  "mcpServers": {
+    "playwright": {
+      "command": "npx",
+      "args": ["-y", "@executeautomation/playwright-mcp-server"]
+    }
+  }
+}
+
+
+# vscode 安装 playwright mcp server
+code --add-mcp '{"name":"playwright","command":"npx","args":["@playwright/mcp@latest"]}'
+
+
+
+
+```
+
+提示词:
+
+```
+使用mcp,将 @星巴克营销活动配置数据.md 中的活动一数据填入 @starbucks_marketing_form.html 
+
+打开哔哩哔哩,搜索 playwright,点击第一条搜索结果,点击播放按钮,播放3s钟之后,点击暂停。
+
+
+
+```
+
+
 ## Reference
 
 [playwright](https://playwright.dev/)官网

+ 11 - 0
docker-compose.yml

@@ -0,0 +1,11 @@
+version: '3.4'
+
+services:
+  playwright:
+    image: jianboy/playwright:latest
+    build:
+      context: .
+      dockerfile: ./Dockerfile
+    ports:
+      - 8001:3000
+# docker run -d -p XXX:XXX -v "C:\Users\Desktop\dockerbuild\fkapi":/app my-playwright-image sh -c "cd /app/fkapi&& python /app/fkapi/main.py"

+ 21 - 0
entrypoint.sh

@@ -0,0 +1,21 @@
+#!/bin/bash
+
+set -e
+
+# 启动 Xvfb(虚拟显示)
+Xvfb :99 -screen 01280x1024x24 -ac +extension GLX +render -noreset &
+
+mode=${1:-interactive}
+case"$mode" in
+    interactive)
+        # 交互模式
+        exec /bin/bash -i
+        ;;
+    daemon)
+       # 运行 Playwright 脚本, 例如:
+        exec python3 -m http.server
+        ;;
+    *)
+        exec "$@"
+        ;;
+esac

+ 17 - 0
test/t1.py

@@ -0,0 +1,17 @@
+from playwright.sync_api import sync_playwright
+
+
+def run() -> None:
+    browser = sync_playwright().start().chromium.launch(headless=False, slow_mo=60)
+    context = browser.new_context()
+    page = context.new_page()
+    page.goto(url)
+    page.get_by_text("登錄").click()
+    page.get_by_placeholder("Please enter your email").click()
+    page.get_by_placeholder("Please enter your email").fill('123')
+    
+    context.close()
+    browser.close()
+    sync_playwright().stop()
+
+run()

+ 21 - 0
test/t2.py

@@ -0,0 +1,21 @@
+from playwright.sync_api import sync_playwright, Playwright
+
+
+def run(playwright: Playwright) -> None:
+    browser = playwright.chromium.launch(headless=False)
+    context = browser.new_context()
+
+    context.tracing.start(snapshots=True, screenshots=True, sources=True)
+
+    page = context.new_page()
+    page.goto('https://playwright.dev')
+    page.screenshot(path='screenshot_test.jpg')
+
+    context.tracing.stop(path='playwright_trace.zip')
+
+    context.close()
+    browser.close()
+
+
+with sync_playwright() as playwright:
+    run(playwright)