playwright

入门

1.安装

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple playwright

# 安装浏览器驱动
playwright install

from time import sleep
from playwright.sync_api import sync_playwright


def login():
    with sync_playwright() as context_manager:
        browser = context_manager.chromium.launch(headless=False)
        context = browser.new_context(no_viewport=True) # 浏览器最大化
        page = context.new_page() 

        page.goto('https://test-sso.jlcerp.com/#/?appId=221884520471289857&redirectUrl=https://test-mh.jlcerp.com/%23/login')


        page.fill('xpath=//input[@placeholder="请输入账号"]', 'erpcs')
        page.fill('xpath=//input[@placeholder="请输入密码"]', 'Kwjx74Tg!d28')
        page.fill('xpath=//input[@placeholder="输入图片验证码"]', '1111')
        page.locator('xpath=//button[@class="el-button el-button--primary login-btn"]').click()



        # page.fill('#kw', 'erpcs')

        sleep(100)

if __name__ == '__main__':
    login()

处理弹窗

方式一:利用context

def demo1():
    context_manager  = sync_playwright().start()
    browser = context_manager.chromium.launch(headless=False)
    context = browser.new_context(no_viewport=True)

    page = context.new_page()
    with page.expect_popup() as new_page_info:
        page.goto('https://test-mh.xxxxxx.com/#/')

        page.fill('//input[@placeholder="请输入账号"]', 'xxx')
        page.fill('//input[@placeholder="请输入密码"]', 'xxx')
        page.fill('//input[@placeholder="输入图片验证码"]', '1111')
        page.locator('xpath=//button[@class="el-button el-button--primary login-btn"]').click()

    print(new_page_info)
    pages = context.pages
    print(pages)

    new_page = new_page_info.value
    page.on("popup", handle_popup)

    loctor = new_page.locator('xpath=//div[text()="FA业务中心"]')
    print(f'loctor:{loctor}')
    loctor.click()
    page.wait_for_timeout(1000)

out:

<playwright._impl._sync_base.EventInfo object at 0x0000025405395780>
[<Page url='about:blank'>, <Page url='https://test-mh.xxx.com/#/login?code=e5e4b3dece594989b7aa0a6bf11ffb43'>]
loctor:<Locator frame=<Frame name= url='https://test-mh.xxx.com/#/login?code=e5e4b3dece594989b7aa0a6bf11ffb43'> selector='xpath=//div[text()="FA业务中心"]'>

方式二:利用page

def demo2():
    context_manager  = sync_playwright().start()
    browser = context_manager.chromium.launch(headless=False)
    context = browser.new_context(no_viewport=True)

    page = context.new_page()
    with context.expect_page() as new_page_info:
        page.goto('https://test-mh.jlcerp.com/#/')

        page.fill('//input[@placeholder="请输入账号"]', 'erpcs')
        page.fill('//input[@placeholder="请输入密码"]', 'Kwjx74Tg!d28')
        page.fill('//input[@placeholder="输入图片验证码"]', '1111')
        page.locator('xpath=//button[@class="el-button el-button--primary login-btn"]').click()

    print(new_page_info)
    pages = context.pages
    print(pages)

    new_page = new_page_info.value
    loctor = new_page.locator('xpath=//div[text()="FA业务中心"]')
    print(f'loctor:{loctor}')
    loctor.click()
    page.wait_for_timeout(1000)

frame

https://blog.csdn.net/qq_23827149/article/details/136020071?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-1-136020071-blog-129613624.235v43pc_blog_bottom_relevance_base8&spm=1001.2101.3001.4242.2&utm_relevant_index=4

官方资料:

https://github.com/microsoft/playwright
https://playwright.dev/python/docs/writing-tests

无头模式:
https://blog.csdn.net/liwenxiang629/article/details/130682736

jianshu.com/p/c4bb093e0f86
https://blog.csdn.net/2301_78276982/article/details/138964151

页面切换
https://blog.csdn.net/qq_35948955/article/details/135671846

https://blog.51cto.com/u_15249893/6104892

https://blog.csdn.net/qq_56271699/article/details/131939951

鼠标操作:
https://blog.csdn.net/qq_46158060/article/details/134378834

get_by_role:
https://blog.csdn.net/seanyang_/article/details/132208499

点击后获取指定接口的响应数据:
https://www.bilibili.com/read/cv27493046/

posted @ 2024-06-19 15:27  chuangzhou  阅读(17)  评论(0编辑  收藏  举报