本地playwright打包docker封装(chrome)
拉取官方镜像:
docker pull mcr.microsoft.com/playwright/python:v1.31.0-focal
运行:
docker run -it --name=python_playwright -v /Users/kaka/miniconda3/envs/playwright_tianmao/project/src:/src mcr.microsoft.com/playwright/python:v1.31.0-focal /bin/bash
本地代码:
查看代码
from playwright.sync_api import Playwright, sync_playwright, expect
# 对象初始化
def run(playwright: Playwright) -> None:
# 用户数据目录的路径,该目录存储浏览器会话数据,如 cookie 和本地存储。
user_data_dir = "/Users/kaka/chrome2"
exec_path = "/ms-playwright/chromium-1048/chrome-linux/chrome"
# 传递给浏览器实例的附加参数。
args = [
'--disable-blink-features=AutomationControlled',
f"--disable-extensions",
f"--disable-popup-blocking",
f"--ignore-certificate-errors",
f"--disable-plugins-discovery",
f'--no-first-run',
f'--no-service-autorun',
f'--no-default-browser-check',
# f'--no-startup-window',
f'--disable-dev-shm-usage',
# f"--disable-extensions-except={cookie_extension},{path_to_extension}",
]
context = playwright.chromium.launch_persistent_context (
user_data_dir,
args=args,
bypass_csp=True,
channel="chrome",
device_scale_factor=1,
devtools=False,
headless=True,
executable_path = exec_path
)
page = context.new_page ()
# 登录网址
login_url = "https://login.taobao.com/member/login.jhtml"
# 打开网页
page.goto (login_url)
# 自适应等待,点击密码登录选项
page.set_default_timeout (2000)
# 账户名
try:
username = page.locator ('#fm-login-id')
username.click ()
username.fill ('账号')
except Exception as e:
print (f"账号名/邮箱/手机号->Exception:{e}")
pass
# 密码
try:
userpass = page.locator ('#fm-login-password')
userpass.click ()
userpass.fill ('密码')
except Exception as e:
print (f"请输入登录密码->Exception:{e}")
pass
# 获取拖动按钮位置并拖动
try:
drop_button = page.frame_locator ("#baxia-dialog-content").locator (
"#nc_1_n1z")
box = drop_button.bounding_box ()
page.mouse.move (box['x'] + box['width'] / 2, box['y'] + box['height'] / 2)
page.mouse.down ()
mov_x = box['x'] + box['width'] / 2 + 260
page.mouse.move (mov_x, box['y'] + box['height'] / 2)
page.mouse.up ()
except Exception as e:
print (f"获取拖动按钮位置并拖动->Exception:{e}")
pass
# 登录按钮
try:
# page.frame_locator("#J_taobao iframe").locator(".fm-submit").click()
page.get_by_role ("button", name="登录").click ()
except Exception as e:
print (f"登录按钮->Exception:{e}")
pass
# 快速进入
try:
# page.frame_locator("#J_taobao iframe").locator(".fm-submit").click()
c = page.get_by_role ("button", name="快速进入")
c.click (force=True)
c.click ()
except Exception as e:
print (f"快速进入->Exception:{e}")
pass
# html就是返回的页面源代码
html = page.content ()
# print (html)
# 直到获取到淘宝会员昵称才能确定是登录成功
page.set_default_timeout (5000)
try:
taobao_name = page.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 ")
print ('获取到登录的淘宝昵称是:' + str (taobao_name.inner_text ()))
except Exception as e:
print (f"未获取到登录的淘宝昵称->Exception:{e}")
pass
with sync_playwright () as playwright:
run (playwright)
测试:
root@2b90a02bbb7c:/# python src/tian_mao.py
获取拖动按钮位置并拖动->Exception:Timeout 2000ms exceeded.
=========================== logs ===========================
waiting for frame_locator("#baxia-dialog-content").locator("#nc_1_n1z")
============================================================
快速进入->Exception:Timeout 2000ms exceeded.
=========================== logs ===========================
waiting for get_by_role("button", name="快速进入")
============================================================
获取到登录的淘宝昵称是:tb4210828_2012
root@2b90a02bbb7c:/#
注意这个:
exec_path = "/ms-playwright/chromium-1048/chrome-linux/chrome"
还有这里:
context = playwright.chromium.launch_persistent_context (
user_data_dir,
args=args,
bypass_csp=True,
channel="chrome",
device_scale_factor=1,
devtools=False,
headless=True,
executable_path = exec_path
)