1. 微軟開源的瀏覽器自動(dòng)化工具-Playwright

        共 3227字,需瀏覽 7分鐘

         ·

        2020-11-02 08:11

        軟開源了一個(gè) Python 項(xiàng)目:Playwright,從此又多了一個(gè)瀏覽器自動(dòng)化工具。之前一直用 selenium 或 splinter。

        Playwright 可通過單個(gè) API?自動(dòng)執(zhí)行?Chromium,Firefox?和?WebKit瀏覽器,支持無頭瀏覽器(headless),Linux、macOS、Windows 下均可以使用,Playwright提供的自動(dòng)化技術(shù)是綠色的功能強(qiáng)大,穩(wěn)定且速度快。


        Playwright 最吸引我的地方在于它可以自己記錄你對(duì)瀏覽器的操作,并將這些操作生成可以執(zhí)行的代碼,這簡直就是神器,大大提升了瀏覽器自動(dòng)化的效率。生成代碼只需要執(zhí)行
        python -m playwright codegen
        此外,它不像 selenium 需要再單獨(dú)安裝瀏覽器驅(qū)動(dòng),它在 pip install 時(shí)就會(huì)安裝瀏覽器的驅(qū)動(dòng)文件。
        pip install playwrightpython -m playwright install
        這將會(huì)安裝 Playwright 和 Chromium,F(xiàn)irefox 和 WebKit 瀏覽器的二進(jìn)制文件,非常方便,需要 Python 3.7 及以上版本。

        還有幾個(gè)亮眼的功能:

        1、Playwright同時(shí)提供同步(阻止)API和異步API。

        它們在功能方面是相同的,只是在使用API的方式上有所不同。

        同步:
        from playwright import sync_playwright
        with sync_playwright() as p: for browser_type in [p.chromium, p.firefox, p.webkit]: browser = browser_type.launch() page = browser.newPage() page.goto('http://whatsmyuseragent.org/') page.screenshot(path=f'example-{browser_type.name}.png') browser.close()
        異步:
        import asynciofrom playwright import async_playwright
        async def main(): async with async_playwright() as p: for browser_type in [p.chromium, p.firefox, p.webkit]: browser = await browser_type.launch() page = await browser.newPage() await page.goto('http://whatsmyuseragent.org/') await page.screenshot(path=f'example-{browser_type.name}.png') await browser.close()
        asyncio.get_event_loop().run_until_complete(main())

        2、集成 pytest 測試:

        def test_playwright_is_visible_on_google(page): page.goto("https://www.google.com") page.type("input[name=q]", "Playwright GitHub") page.click("input[type=submit]") page.waitForSelector("text=microsoft/Playwright")

        3、交互模式運(yùn)行:

        >>> from playwright import sync_playwright>>> playwright = sync_playwright().start()
        # Use playwright.chromium, playwright.firefox or playwright.webkit# Pass headless=False to see the browser UI>>> browser = playwright.chromium.launch()>>> page = browser.newPage()>>> page.goto("http://whatsmyuseragent.org/")>>> page.screenshot(path="example.png")>>> browser.close()>>> playwright.stop(

        4、執(zhí)行 JS 代碼:

        from playwright import sync_playwright
        with sync_playwright() as p: browser = p.firefox.launch() page = browser.newPage() page.goto('https://www.example.com/') dimensions = page.evaluate('''() => { return { width: document.documentElement.clientWidth, height: document.documentElement.clientHeight, deviceScaleFactor: window.devicePixelRatio } }''') print(dimensions) browser.close()

        5、中斷網(wǎng)絡(luò)請求:

        from playwright import sync_playwright
        with sync_playwright() as p: browser = p.chromium.launch() page = browser.newPage()
        def log_and_continue_request(route, request): print(request.url) route.continue_()
        # Log and continue all network requests page.route('**', lambda route, request: log_and_continue_request(route, request))
        page.goto('http://todomvc.com') browser.close()
        ?

        官方文檔暫時(shí)還是 Node.js 版本,不過正在轉(zhuǎn)換成 Python 版本,API的調(diào)用方式相當(dāng)一致,現(xiàn)在看 Node.js 版本的文檔來編碼也是無障礙的。

        官方文檔:https://playwright.dev/
        GitHub 倉庫:https://github.com/microsoft/playwright-python

        綜上,感覺比 selenium 更好用啦,準(zhǔn)備入坑。如果覺得文章對(duì)你有用,歡迎點(diǎn)贊、轉(zhuǎn)發(fā)、關(guān)注,謝謝支持。

        瀏覽 28
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
          
          

            1. 手机看片欧美+日韩+国产 | 精品人妻无码一区二区三区91电影 | 你夹的太紧了,我拔不出来了 | 竟然挤出喷出奶水乳奴 | 色婷婷国产精品免费视频 |