<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          微軟開源的瀏覽器自動化工具-Playwright

          共 3227字,需瀏覽 7分鐘

           ·

          2020-11-02 08:11

          軟開源了一個 Python 項目:Playwright,從此又多了一個瀏覽器自動化工具。之前一直用 selenium 或 splinter。

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


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

          還有幾個亮眼的功能:

          1、Playwright同時提供同步(阻止)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、交互模式運行:

          >>> 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()
          ?

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

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

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

          瀏覽 28
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲天堂中文字幕 | 美女做爱网站 | 黄色在线观看视频 | 国产看片网站 | 天天射日日舔 |