首页 > 其他分享 > 本地playwright打包docker封装(chrome)

本地playwright打包docker封装(chrome)

时间:2023-03-16 18:35:42浏览次数:38  
标签:box Exception playwright locator chrome -- docker page

拉取官方镜像:

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
    )

 

标签:box,Exception,playwright,locator,chrome,--,docker,page
From: https://www.cnblogs.com/kaka0318/p/17223754.html

相关文章

  • Docker安装及初始化
    1Docker安装方法1.1(推荐)根据官方指引:InstallDockerEngine安装;方法1.2(不推荐)使用iEDA仓库中的build脚本安装,如下:#内部仓库,等待开源2初始化Docker设置方......
  • docker运行的playwright记录
    下载官方镜像:https://playwright.dev/python/docs/docker使用命令;dockerpullmcr.microsoft.com/playwright/python:v1.31.0-focal然后运行:dockerrun-it--rm-......
  • docker-compose编排环境报错
    docker-composeup拉取环境出现如下报错,看到Timeout以为是网络不好,其实不然,用vim/etc/docker/daemon.json添加新的镜像地址,再次docker-compose的时候就顺利进行了。......
  • 使用Docker快速部署一个Net项目
    前言Docker可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化。优点Web应用的自动化打包和发......
  • docker mysql8.0 启动,挂数据卷,定时备份,恢复~
    安装mysql从mysql社区版的官方源去拉取镜像:mysql/mysql-server-DockerImage|DockerHubdockerrun--name=mysql1-dmysql/mysql-server:latest 镜像起来之后,mys......
  • 新版谷歌Chrome取消对PPAPI插件支持后,浏览器网页打开编辑保存微软Office、金山WPS文档
    最近陆续看到一些大学发布公告,谷歌Chrome取消了对PPAPI插件支持,导致某些在线Office厂家产品将无法在谷歌Chrome107及以上版本运行,被迫更换浏览器或者使用低版本浏览器苟延......
  • WINDOWS 10更改WSL DOCKER镜像目录
    转载申明:此文档转载自:https://www.cnblogs.com/friend/p/16722869.html 现在Docker Desktop默认使用WSL2来运行,而不是以前的Hyper-V。WSLWSL:适用于Linux的Window......
  • centos7 安装docker 错误解决
    yum-config-manager--add-repohttp://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repoyum-yinstalldocker-ce安装报错:错误:软件包:2:container-selinux-2......
  • docker 部署 xxl-job 详细步骤
    1、先安装mysqldockerpullmysql2、运行mysql容器(端口3306容器名称mysql密码123456)dockerrun-d--namemysql-eMYSQL_ROOT_PASSWORD=123456-p3306:3......
  • docker login "Password change required but no TTY available."
    docker login"PasswordchangerequiredbutnoTTYavailable."报错现象 原因原因为账号密码过期或登录方式不支持解决方式一sudodockerlogin-uuser-px......