How to use sync_playwright method in Playwright Python

Best Python code snippet using playwright-python

Playwright usage.py

Source:Playwright usage.py Github

copy

Full Screen

...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)...

Full Screen

Full Screen

playwrightdemo.py

Source:playwrightdemo.py Github

copy

Full Screen

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

Full Screen

Full Screen

Playwright_proxy.py

Source:Playwright_proxy.py Github

copy

Full Screen

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

Full Screen

Full Screen

main.py

Source:main.py Github

copy

Full Screen

...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:...

Full Screen

Full Screen

Playwright tutorial

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.

Chapters:

  1. What is Playwright : Playwright is comparatively new but has gained good popularity. Get to know some history of the Playwright with some interesting facts connected with it.
  2. How To Install Playwright : Learn in detail about what basic configuration and dependencies are required for installing Playwright and run a test. Get a step-by-step direction for installing the Playwright automation framework.
  3. Playwright Futuristic Features: Launched in 2020, Playwright gained huge popularity quickly because of some obliging features such as Playwright Test Generator and Inspector, Playwright Reporter, Playwright auto-waiting mechanism and etc. Read up on those features to master Playwright testing.
  4. What is Component Testing: Component testing in Playwright is a unique feature that allows a tester to test a single component of a web application without integrating them with other elements. Learn how to perform Component testing on the Playwright automation framework.
  5. Inputs And Buttons In Playwright: Every website has Input boxes and buttons; learn about testing inputs and buttons with different scenarios and examples.
  6. Functions and Selectors in Playwright: Learn how to launch the Chromium browser with Playwright. Also, gain a better understanding of some important functions like “BrowserContext,” which allows you to run multiple browser sessions, and “newPage” which interacts with a page.
  7. Handling Alerts and Dropdowns in Playwright : Playwright interact with different types of alerts and pop-ups, such as simple, confirmation, and prompt, and different types of dropdowns, such as single selector and multi-selector get your hands-on with handling alerts and dropdown in Playright testing.
  8. Playwright vs Puppeteer: Get to know about the difference between two testing frameworks and how they are different than one another, which browsers they support, and what features they provide.
  9. Run Playwright Tests on LambdaTest: Playwright testing with LambdaTest leverages test performance to the utmost. You can run multiple Playwright tests in Parallel with the LammbdaTest test cloud. Get a step-by-step guide to run your Playwright test on the LambdaTest platform.
  10. Playwright Python Tutorial: Playwright automation framework support all major languages such as Python, JavaScript, TypeScript, .NET and etc. However, there are various advantages to Python end-to-end testing with Playwright because of its versatile utility. Get the hang of Playwright python testing with this chapter.
  11. Playwright End To End Testing Tutorial: Get your hands on with Playwright end-to-end testing and learn to use some exciting features such as TraceViewer, Debugging, Networking, Component testing, Visual testing, and many more.
  12. Playwright Video Tutorial: Watch the video tutorials on Playwright testing from experts and get a consecutive in-depth explanation of Playwright automation testing.

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful