1. Introduction

Recently, Microsoft opened source a very powerful Python automation dependency library called Caller-Python

It supports all major browsers, including Chrome, Firefox, Safari, And Microsoft Edge, and runs in either headless or headless modes

Caller-python provides both synchronous and asynchronous apis that can be used in conjunction with the Pytest testing framework, and supports automated scripting on the browser side

Project Address:

Github.com/microsoft/p…

2. Prepare

We only need two steps before we actually do it

Step 1, install the Offended python dependency library

Install the dependency library pip3 install ErrorCopy the code

Step 2: Install a major browser driver

This will download the Chromeium, Firefox, and Webkit browser drivers to the local

Install the browser driver Python -m Incorrectly installCopy the code

3. Practice

3-1 Recording scripts

Let’s first look at the command description for the recording script

Among them

  • Python -m Offended CodeGen Recording script

  • –help Help documentation

  • -o Specifies the directory where the automation script is generated

  • — Target Scripting language, including JS and Python. The values are Python and javascript, respectively

  • -b Specifies the browser driver

Such as:

1. Py (optional) Webkit (default: webKit, optional) # Follow the target site to open (default: just open the browser, Optional) Python -m Offended Codegen --target python -o '1.py' -b webkit https://www.baidu.comCopy the code

Next, simulate a search in the browser and close the browser

Finally, the automated script is automatically generated and saved to a file

from playwright import sync_playwright def run(playwright): browser = playwright.webkit.launch(headless=False) context = browser.newContext() # Open new page page = context.newPage() # Go to https://www.baidu.com/ page.goto("https://www.baidu.com/") # Fill input[name="wd"] page.fill("input[name=\"wd\"]", "AirPython") # Press Enter # with page.expect_navigation(url="https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=AirPython&fenlei=256&rsv _pq=a1739d870005eec3&rsv_t=e640wwS33ra1Koivxvy1WyTxyknRwnllWiw4JBqIYd/KUN/WKpWLtL2b2+0&rqlang=cn&rsv_enter=1&rsv_dl=tb&r sv_sug3=21&rsv_sug1=18&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&inputT=6199&rsv_sug4=6199"): with page.expect_navigation(): page.press("input[name=\"wd\"]", "Enter") # Close page page.close() # --------------------- context.close() browser.close() with sync_playwright() as playwright: run(playwright)Copy the code

3-2 synchronization

The key word for synchronization is sync_Offender

For example, we use three browser cores in turn to open the browser, then baidu, then in the search screen shot, and finally close the browser

From time import sleep from Hereafter import sync_down () as p For browser_type in [p.chrome, p.firefox, p.ebkit]: # specifies header mode, Browser = browser_type. Launch (headless=False) page = browser.newpage () page. Goto ('http://baidu.com') # Perform a search operation page.fill("input[name=\"wd\"]", "AirPython") with page.expect_navigation(): page.press("input[name=\"wd\"]", Screenshot (path=f' example_{browser_type. Name}.png' # Close browser. Close ()Copy the code

It should be noted that The Caller-Python built-in API basically covers common automation operations

3-3 asynchronous

The keyword of an asynchronous step is async_Offender

In conjunction with Asyncio, we perform the above operations simultaneously

Import async def main(): async with async_offended () as p: for browser_type in [p.chromium, p.firefox, p.webkit]: # specifies header mode, Easy to see browser = await browser_type. Launch (headless=False) page = await browser.newPage() await Await page. Fill ("input[name=\"wd\"]", "AirPython") await page.press("input[name=\"wd\"]", "Enter") # wait for page to load fully await page. WaitForSelector ("text= ") # screenshot await page.screenshot(path=f'example-{browser_type.name}.png') await browser.close() asyncio.get_event_loop().run_until_complete(main())Copy the code

4. The last

In fact, Offended is a cross-language automation framework that supports Python, Java, JS, and more

Compared with the traditional automation framework Selenium, The Offender is more concise and powerful in terms of Context and API usage. More detailed functions can be unlocked by reading the original article

More Python automation, crawler related original technology dry goods, you can pay attention to the public number AirPython to unlock!

Recommended reading

How to Automate teamwork in Python

For these automation scenarios, batch processing can completely replace Python

I solved my little sister’s wechat anxiety perfectly with a few lines of Automated Python scripts