Playwigth安装和基本用法
1.安装playwright
pip install playwright
2.安装浏览器驱动文件(自动安装Chromium、Firefox、WebKit等浏览器的驱动文件)
python -m playwright install
3.录制命令codegen
通过python -m playwright codegen --help 可查看所有用法
查看里面的options含义:
-o:将录制的脚本保存到一个文件的文件名称
--target:指定生成的脚本语言,默认为python,目前支持python和JS
-b:指定浏览器驱动
例如:
使用谷歌浏览器打开百度搜索,脚本存放文件名称为open_baidu.py,指定脚本语言为python
命令是:
python -m playwright codegen --target python -o open_baidu.py -b chromium https://www.baidu.com
输入命令后,自动打开浏览器进入百度页面,后续操作的行为都会进行录制,结束时自动将python脚本存放在open_baidu.py
生成脚本内容如下:
from playwright.sync_api import Playwright, sync_playwright, expect def run(playwright: Playwright) -> None: browser = playwright.chromium.launch(headless=False) context = browser.new_context() # Open new page page = context.new_page() # Go to https://www.baidu.com/ page.goto("https://www.baidu.com/") # Click input[name="wd"] page.locator("input[name=\"wd\"]").click() # Fill input[name="wd"] page.locator("input[name=\"wd\"]").fill("李文吉") # Click text=百度一下 page.locator("text=百度一下").click() # expect(page).to_have_url("https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E6%9D%8E%E6%96%87%E5%90%89&fenlei=256&rsv_pq=c84c4e7200038459&rsv_t=3df1jE2dzIn%2B%2B8YrwSHy0GecsMRRENBqKaNMLIEN8GUm4K9thFDdW4FnGL0&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_sug3=13&rsv_sug1=12&rsv_sug7=100&rsv_btype=i&prefixsug=%25E6%259D%258E%25E6%2596%2587%25E5%2590%2589&rsp=3&inputT=7430&rsv_sug4=9727") # Click text=李文吉lee的微博_微博 # with page.expect_navigation(url="https://weibo.com/u/2472235200"): with page.expect_navigation(): with page.expect_popup() as popup_info: page.locator("text=李文吉lee的微博_微博").click() page1 = popup_info.value # Go to https://weibo.com/u/2472235200 page1.goto("https://weibo.com/u/2472235200") # Click text=热门微博 # with page1.expect_navigation(url="https://weibo.com/hot/weibo/102803"): with page1.expect_navigation(): page1.locator("text=热门微博").click() # expect(page1).to_have_url("https://weibo.com/") # Click .LoginPop_btn_31Lh2 page1.locator(".LoginPop_btn_31Lh2").click() # Close page page1.close() # Close page page.close() # --------------------- context.close() browser.close() with sync_playwright() as playwright: run(playwright)
4. 支持同步
''' project_name:AutoTestSelenium name:playwright_test time:2022/4/23-13:20''' from playwright.sync_api import sync_playwright, Playwright from playwright.async_api import async_playwright import time import asyncio # 同步实例1 def sync_open_baidu(playwright: Playwright) -> None: browser = playwright.chromium.launch(headless=False) # headless 默认时True:无头浏览器 context = browser.new_context() page = context.new_page() page.goto(url='http://www.baidu.com') time.sleep(5) page.locator('//input[@id="kw"]').fill('李文吉') time.sleep(5) page.locator('text=百度一下').click() time.sleep(5) print(page.title()) browser.close() # 同步实例2 def sync_open_dyj(playwright: Playwright) -> None: browser = playwright.chromium.launch(headless=False) # headless 默认时True:无头浏览器 context = browser.new_context() page = context.new_page() page.goto(url='http://www.baidu.com') time.sleep(5) page.locator('//input[@id="kw"]').fill('邓亚娟') time.sleep(5) page.locator('text=百度一下').click() time.sleep(5) print(page.title()) browser.close() if __name__ == '__main__': # 同步方式调用 with sync_playwright() as playwright: sync_open_baidu(playwright) sync_open_dyj(playwright)
5.支持异步
''' project_name:AutoTestSelenium name:playwright_test time:2022/4/23-13:20 ''' from playwright.sync_api import sync_playwright, Playwright from playwright.async_api import async_playwright import time import asyncio #方式1:异步实例1 async def async_open_baidu(): async with async_playwright() as playwright: browser = await playwright.chromium.launch(headless=False) page = await browser.new_page() await page.goto('http://www.baidu.com') await page.locator('//input[@id="kw"]').fill('李文吉') await page.locator('text=百度一下').click() print(await page.title()) await browser.close() #方式1:异步实例2 async def async_open_dyj(): async with async_playwright() as playwright: browser = await playwright.chromium.launch(headless=False) page = await browser.new_page() await page.goto('http://www.baidu.com') await page.locator('//input[@id="kw"]').fill('邓亚娟') await page.locator('text=百度一下').click() print(await page.title()) await browser.close() #方式2异步 实例创建page对象 async def run(url): async with async_playwright() as playwright: # create a chromium browser instance chromium = playwright.chromium # browser = await chromium.launch() # create a bowser instance which headless is true browser = await chromium.launch(headless=False) # create two isolated browser contexts user_context = await browser.new_context() page = await user_context.new_page() await page.goto(url) # await page2.goto('https://www.baidu.com') #方式2异步实例调用 async def main(): tasks = [] urls = ['https://www.baidu.com', 'https://www.taobao.com', 'https://www.jd.com'] for url in urls: task = asyncio.ensure_future(run(url)) tasks.append(task) await asyncio.gather(*tasks) if __name__ == '__main__': # 方式2异步调用 # asyncio.run(main()) # 方式1异步调用 # ren = [async_open_baidu(), async_open_dyj()] # loops = asyncio.get_event_loop() # loops.run_until_complete(asyncio.wait(ren))