Best Python code snippet using playwright-python
Playwright usage.py
Source:Playwright usage.py
...4import re5import time6'''åºæ¬ä½¿ç¨'''7'''åæ¥æ¨¡å¼'''8# with sync_playwright() as p:9# for browser_type in [p.chromium,p.firefox,p.webkit]:10# browser = browser_type.launch(headless=False)11# page = browser.new_page()12# page.goto('https://www.baidu.com')13# page.screenshot(path=f'screenshot-{browser_type.name}.png')14# print(page.title())15# browser.close()16'''å¼æ¥æ¨¡å¼'''17# async def main():18# async with async_playwright() as p:19# for browser_type in [p.chromium,p.firefox,p.webkit]:20# browser = await browser_type.launch()21# page = await browser.new_page()22# await page.goto('https://www.baidu.com')23# await page.screenshot(path=f'screenshot-{browser_type.name}.png')24# print(await page.title())25# await browser.close()26#27# asyncio.run(main())28'''代ç çæ'''29'''æ¯æ移å¨ç«¯æµè§å¨'''30# with sync_playwright() as p:31# iphone_12_pro_max = p.devices['iPhone 12 Pro Max']32# browser = p.webkit.launch(headless=False)33# context = browser.new_context(34# **iphone_12_pro_max,35# locale='zh-CN'36# )37# page = context.new_page()38# page.goto('https://www.whatismybrowser.com/')39# page.wait_for_load_state(state='networkidle')40# page.screenshot(path='browser-iphone.png')41# browser.close()42'''éæ©å¨'''43'''常ç¨æä½æ¹æ³'''44'''äºä»¶çå¬'''45# def on_response(response):46# # print(f'Status {response.status}:{response.url}')47# if '/api/movie/' in response.url and response.status == 200:48# print(response.json())49#50#51# with sync_playwright() as p:52# browser = p.chromium.launch(headless=False)53# page = browser.new_page()54# page.on('response', on_response)55# page.goto('https://spa6.scrape.center/')56# page.wait_for_load_state('networkidle')57# browser.close()58'''è·å页é¢æºä»£ç '''59with sync_playwright() as p:60 browser = p.chromium.launch(headless=False)61 page = browser.new_page()62 page.goto('https://660e.com/?url=https://v.qq.com/x/cover/mzc00200lxzhhqz/d0040q5zhb7.html')63 page.wait_for_load_state('networkidle')64 html = page.content()65 print(html)66 browser.close()67'''ç½ç»å«æ'''68# with sync_playwright() as p:69# browser = p.chromium.launch(headless=False)70# page = browser.new_page()71#72# def cancel_request(route, request):73# route.abort()74#75# page.route(re.compile(r"(\.png)|(\.jpg)"), cancel_request)76# page.goto("https://spa6.scrape.center/")77# page.wait_for_load_state('networkidle')78# page.screenshot(path='no_picture.png')79# time.sleep(10)80# browser.close()81#82# with sync_playwright() as p:83# browser = p.chromium.launch(headless=False)84# page = browser.new_page()85#86# def modify_response(route, request):87# route.fulfill(path="./custom_response.html")88#89# page.route('/', modify_response)90# page.goto("https://spa6.scrape.center/")91# time.sleep(10)...
playwrightdemo.py
Source:playwrightdemo.py
1from bs4.element import SoupStrainer2from playwright.sync_api import sync_playwright3from bs4 import BeautifulSoup4with sync_playwright() as p:5 browser = p.chromium.launch(headless=False, slow_mo=50)6 # browser = p.chromium.launch() # remove the browser popup7 page = browser.new_page()8 page.goto('https://demo.opencart.com/admin/')9 page.fill('input#input-username', 'demo')10 page.fill('input#input-password', 'demo')11 page.click('button[type=submit]')12 page.is_visible('div.tile-body')13 html = page.inner_html('#content')14 soup = BeautifulSoup(html, 'html.parser')15 total_orders = soup.find('h2', {'class': 'pull-right'}).text16 print(f'total orders = {total_orders}')17 browser.close()18#Example219from playwright.sync_api import sync_playwright20with sync_playwright() as p:21 for browser_type in [p.chromium, p.firefox, p.webkit]:22 browser = browser_type.launch()23 page = browser.new_page()24 page.goto('http://whatsmyuseragent.org/')25 page.screenshot(path=f'example-{browser_type.name}.png')26 browser.close()27import asyncio28from playwright.async_api import async_playwright29async def main():30 async with async_playwright() as p:31 browser = await p.chromium.launch()32 page = await browser.new_page()33 await page.goto("http://playwright.dev")34 print(await page.title())35 await browser.close()...
Playwright_proxy.py
Source:Playwright_proxy.py
1from playwright.sync_api import sync_playwright2'''http代ç'''3# with sync_playwright() as p:4# browser = p.chromium.launch(headless=False, proxy={5# 'server': 'http://127.0.0.1:4780'6# })7# page = browser.new_page()8# page.goto('https://httpbin.org/get')9# print(page.content())10# browser.close()11'''socks代ç'''12# with sync_playwright() as p:13# browser = p.chromium.launch(headless=False, proxy={14# 'server': 'socks5://127.0.0.1:4781'15# })16# page = browser.new_page()17# page.goto('https://httpbin.org/get')18# print(page.content())19# browser.close()20'''é认è¯ç代ç'''21with sync_playwright() as p:22 browser = p.chromium.launch(headless=False, proxy={23 'server': 'http://127.0.0.1:4780',24 'username': 'foo',25 'password': 'bar'26 })27 page = browser.new_page()28 page.goto('https://httpbin.org/get')29 print(page.content())...
main.py
Source:main.py
...5 if "https://live.douyin.com/webcast/im/fetch/" in resp.url:6 response = message_pb2.Response()7 response.ParseFromString(resp.body())8 utils.decodeMsg(response.messages)9browsers = sync_playwright().start().chromium.launch(headless=True)10page = browsers.new_page()11page.on("response", response)12page.goto(13 "https://live.douyin.com/296295439179",14)15while True:...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!