This is the fourth day of my participation in Gwen Challenge
What is a Playwright – python
The ourselves can be automated fast, reliably, and powerfully in all modern browsers.
This guide covers these key differentiators to help you choose the right tool for automated testing.
-
Support for all browsers
-
Fast and reliable execution
-
Strong automation capability
-
Integrate with your workflow
-
limit
-
Release notes
Supports multiple browsers (Chromium, Firefox, WebKit and FFMPEG are installed in Python version 3.8)
Test on Chromium, Firefox, and WebKit. Ourselves has full apis for all modern browsers, including Google Chrome and Microsoft Edge (with Chromium), Apple Safari (with WebKit) and Mozilla Firefox.Copy the code
Cross-platform WebKit testing. Down the drain, We test your app's behavior in Apple Safari using a WebKit build for Windows, Linux, and macOS. Test locally and on CI.Copy the code
Test the phone. ** Test your responsive Web application in a mobile Web browser using device emulation.Copy the code
Headless and headless. The ourselves may support both headless (sans browser UI) and headed (with browser UI) modes in all browsers and on all platforms. Headed is great for debugging, while Headless is faster for CI/ cloud execution.Copy the code
Fast and reliable execution
Automatic wait API. The ourselves will automatically wait for the elements to be ready. This improves reliability and simplifies test writing.Copy the code
No time-out automation. The ourselves receives browser signals, such as web requests, page navigation and page loading events, to eliminate the need for erratic sleep timeouts.Copy the code
Lean parallelization with browser context. ** Reuse a single browser instance for multiple parallel, isolated execution environments with browser context.Copy the code
Elastic element selector. ** Playwrights can rely on user-facing strings, such as text content and accessibility tags, to select elements. These strings are more elastic than selectors tightly coupled to DOM structures.Copy the code
Powerful automation functions
Multiple domains, pages, and frames. The ourselves is an out-of-process automation driver that is not limited by the scope of in-page JavaScript execution and can automate multi-page scenarios.Copy the code
Strong network control. The ourselves has introduced context-scoped network intercepts to stub and simulate network requests.Copy the code
Modern networking features. The ourselves may support network components through shadow penetration selectors, geolocation, permissions, web workers, and other modern web apis.Copy the code
The ability to cover all scenarios. ** Supports file downloads and uploads, out-of-process IFrames, native input events, and even dark mode.Copy the code
The installation
Preparing for Python3.8, I used python3.9 and was stuck in the last step of installing Google drive, and there was no good explanation on the web. When I was about to give up, I re-installed python3.8 and set up a ladder, and it worked. So if your installation fails, try using python3.8
Create a New Python project
Install dependencies
pip install playwright
Copy the code
python -m playwright install
Copy the code
The installation is complete
use
The official document: playwright. Dev/python/docs…
Github address: github.com/microsoft/p…
Let’s take a look at the official documentation
Read link title
from playwright.sync_api import sync_playwright with sync_playwright() as p: # choose browser browser = p.c hromium. Launch () # to open the page jump to https://blog.csdn.net/tangcv page = the new_page () Page. Goto (" https://blog.csdn.net/tangcv ") # print label print (page. The title ()) the close ()Copy the code
The offender supports two variants of the API: synchronous and asynchronous.
If your modern project uses Asyncio, you should use the Async API:
import asyncio
from playwright.async_api import async_playwright
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch()
page = await browser.new_page()
await page.goto("https://blog.csdn.net/tangcv")
print(await page.title())
await browser.close()
asyncio.run(main())
Copy the code
Screen capture
Slows down browser access
chromium.launch(headless=False, slow_mo=50)
Copy the code
Record script
The COMMAND-LINE interface (CLI) can be used to record user interactions and generate Python code.
The browser opens blog.csdn.net/tangcv
playwright codegen https://blog.csdn.net/tangcv
Copy the code
All browser operations are scripted
from playwright.sync_api import sync_playwright def run(playwright): browser = playwright.chromium.launch(headless=False) context = browser.new_context() # Open new page page = context.new_page() # Go to https://www.wikipedia.org/ page.goto("https://www.wikipedia.org/") # Go to https://mp.weixin.qq.com/s/-i8E_jeApjQ2bGASk-ai9w page.goto("https://mp.weixin.qq.com/s/-i8E_jeApjQ2bGASk-ai9w") # Click Nth-child (169) page. Click ("p:nth-child(169)") # click Click ("text= assert ") # assert page.url == "https://mp.weixin.qq.com/s?__biz=MzI3NjYxMzI1NQ==&mid=2247484610&idx=2&sn=cafaa6f7cf2287b6c06fecf1b95eb8d8&chksm=eb7395 2adc041c3c6fd3863ecf7914809026092b32286138e2b959035b848f24bc5f520e167c&scene=21#wechat_redirect" # Click Page. Click ("p:has-text(\" 24-72 hours \")") # click span:has-text(" 新 连 接 地 址 ") Page. Click ("img[Alt =" image "]") # click text= "img Fuwuhao911 focus on black technology software, the latest film and television resources, takeaway coupons, e-commerce coupons, and a variety of charging resources 1 weeks ago Page. Click ("text= program factory program factory wechat fuwuhao911 features Focus on black software technology, the latest film and television resources, take-away coupons, electricity, coupons, and fees resources 1 week ago ") # Go to https://mp.weixin.qq.com/s/-i8E_jeApjQ2bGASk-ai9w page.goto("https://mp.weixin.qq.com/s/-i8E_jeApjQ2bGASk-ai9w") # Click #page-content page.click("#page-content") # --------------------- context.close() browser.close() with sync_playwright() as playwright: run(playwright)Copy the code