Playwright Learning Experience
Introduction: Learning experience of playwright automation framework
Note: I am using a virtual environment here
1. Playwright official website (very detailed)
playwright official documentation (python)
image-1660994177573
2. The advantages of playwright
Usage Summary: Out of the Box
Support recording, clear script, easy maintenance
1. Support both synchronous and asynchronous methods
2. No need to download driver for each browser
3. Relatively more context than selenium
4. Support headless browser
5. Support opening developer tools devtools when running scripts
6. You can use the traditional positioning method, or you can use playwright's own method or custom method
7. Faster to start and execute than selenium
8. The bottom layer of selenium is http single communication, and playwright is based on websocket two-way communication
9.playwright is automatic waiting, no need to do too much processing
10. Flexible and convenient, multi-page switching without iframe
11. For methods or classes that are not well understood, you can understand the usage process by recording
12. High playback efficiency and high return efficiency
13. I don't think secondary packaging is needed for the high availability and stability of the underlying layer.
14. Support python, java, js, ts, C# languages
3. Record the script
1. When I don't know the usage process of some methods, I will record the script to view the script content
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.gaojs.com.cn/
page.goto("https://www.gaojs.com.cn/")
# Click text=About the author >> nth=0
page.locator("text=About the author").first.click()
page.wait_for_url("https://www.gaojs.com.cn/s/about")
# Click text=Image bed >> nth=0
with page.expect_popup() as popup_info:
page.locator("text=picture bed").first.click()
page1 = popup_info.value
page.wait_for_url("http://image.gaojs.com.cn/explore/trending")
# Click text=Explore
page1.locator("text=Explore").click()
# Click text=Recent
page1.locator("text=latest").click()
page1.wait_for_url("http://image.gaojs.com.cn/explore/recent")
# Click a:has-text("Music") >> nth=0
page.wait_for_url("https://www.gaojs.com.cn/s/music")
# Click svg >> nth=0
page.locator("svg").first.click()
# Close page
page1.close()
# Close page
page.close()
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
2. Click to jump to the new page (can be known from the content of recorded script 1)
# Click text=Image bed >> nth=0
with page.expect_popup() as popup_info:
page.locator("text=picture bed").first.click()
page1 = popup_info.value
# This needs to be changed, the recording is page, but it is actually page1
page1.wait_for_url("http://image.gaojs.com.cn/explore/trending")
# Click text=Explore
page1.locator("text=Explore").click()
Fourth, write the demo script
# coding=utf-8
"""
@Project : playwright_env
@File : test.py
@Author: gaojs
@Date: 2022/8/20 18:07
@Blogs : https://www.gaojs.com.cn
"""
from playwright.sync_api import Playwright, sync_playwright, expect
import playwright
import time
# Create the playwright object
playwright = sync_playwright().start()
# headless: true by default, headless mode # devtools is false by default: developer tools are off by default
# Browser object:, args=["--start-maximized"] Maximization does not take effect
browser = playwright.chromium.launch(headless=False, devtools=False)
# context manager object
context = browser.new_context(viewport={'width': 1920, 'height': 1080})
# page object
page1 = context.new_page()
page1.set_default_navigation_timeout(20000)
# open browser
page1.goto('https://www.gaojs.com.cn')
# Reload
page1.reload()
# Wait for an element on the page to appear
page1.wait_for_selector('//*[@id="Joe"]/header/div[1]/div/nav/a[7]')
# page1.locator().click()
# Click text=Image bed >> nth=0
with page1.expect_popup() as popup_info:
page1.locator("text=picture bed").first.click()
page2 = popup_info.value
page2.wait_for_url("http://image.gaojs.com.cn/explore/trending")
# Click text=Explore
page2.locator("text=Explore").click()
# d Wait for the search button to appear in the upper right corner of the page
page2.click("text=latest")
time.sleep(5)
# Close page
page1.close()
# Close page
page2.close()
# ---------------------
context.close()
browser.close()
5. Recording case (playback for a little debugging)
1. Automatically send blogs
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.gaojs.com.cn/admin/index.html
page.goto("https://www.gaojs.com.cn/admin/index.html")
# Go to https://www.gaojs.com.cn/admin/index.html#/
page.goto("https://www.gaojs.com.cn/admin/index.html#/")
# Go to https://www.gaojs.com.cn/admin/index.html#/login?redirect=%2Fdashboard
page.goto("https://www.gaojs.com.cn/admin/index.html#/login?redirect=%2Fdashboard")
# Click text=Shorthand Publish >> textarea
page.locator("text=Shorthand Post >> textarea").click()
# Fill text=Shorthand Post >> textareapage.locator("text=Shorthand Release >> textarea").fill("playwright automated advanced learning, Microsoft's new generation artifact!!!")
# Click button:has-text("Publish")
page.wait_for_url("https://www.gaojs.com.cn/admin/index.html#/posts/list?page=0&size=10&statuses=PUBLISHED&statuses=DRAFT&statuses=INTIMATE")
# Click span:nth-child(4)
page.locator("span:nth-child(4)").click()
# Click a:has-text("Profile")
page.wait_for_url("https://www.gaojs.com.cn/admin/index.html#/user/profile")
# Click div:has-text("system") >> nth=2
# Click a:has-text("Blog Settings")
page.wait_for_url("https://www.gaojs.com.cn/admin/index.html#/dashboard")
# Click span:nth-child(4)
page.locator("span:nth-child(4)").click()
# Click text=Sign out
page.locator("text=logout").click()
# Click button:has-text("OK")
page.wait_for_url("https://www.gaojs.com.cn/admin/index.html#/login?redirect=%2Fdashboard")
# Close page
page.close()
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
image-1660999546286
Note: I am using a virtual environment here
1. Playwright official website (very detailed)
playwright official documentation (python)
image-1660994177573
2. The advantages of playwright
Usage Summary: Out of the Box
Support recording, clear script, easy maintenance
1. Support both synchronous and asynchronous methods
2. No need to download driver for each browser
3. Relatively more context than selenium
4. Support headless browser
5. Support opening developer tools devtools when running scripts
6. You can use the traditional positioning method, or you can use playwright's own method or custom method
7. Faster to start and execute than selenium
8. The bottom layer of selenium is http single communication, and playwright is based on websocket two-way communication
9.playwright is automatic waiting, no need to do too much processing
10. Flexible and convenient, multi-page switching without iframe
11. For methods or classes that are not well understood, you can understand the usage process by recording
12. High playback efficiency and high return efficiency
13. I don't think secondary packaging is needed for the high availability and stability of the underlying layer.
14. Support python, java, js, ts, C# languages
3. Record the script
1. When I don't know the usage process of some methods, I will record the script to view the script content
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.gaojs.com.cn/
page.goto("https://www.gaojs.com.cn/")
# Click text=About the author >> nth=0
page.locator("text=About the author").first.click()
page.wait_for_url("https://www.gaojs.com.cn/s/about")
# Click text=Image bed >> nth=0
with page.expect_popup() as popup_info:
page.locator("text=picture bed").first.click()
page1 = popup_info.value
page.wait_for_url("http://image.gaojs.com.cn/explore/trending")
# Click text=Explore
page1.locator("text=Explore").click()
# Click text=Recent
page1.locator("text=latest").click()
page1.wait_for_url("http://image.gaojs.com.cn/explore/recent")
# Click a:has-text("Music") >> nth=0
page.wait_for_url("https://www.gaojs.com.cn/s/music")
# Click svg >> nth=0
page.locator("svg").first.click()
# Close page
page1.close()
# Close page
page.close()
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
2. Click to jump to the new page (can be known from the content of recorded script 1)
# Click text=Image bed >> nth=0
with page.expect_popup() as popup_info:
page.locator("text=picture bed").first.click()
page1 = popup_info.value
# This needs to be changed, the recording is page, but it is actually page1
page1.wait_for_url("http://image.gaojs.com.cn/explore/trending")
# Click text=Explore
page1.locator("text=Explore").click()
Fourth, write the demo script
# coding=utf-8
"""
@Project : playwright_env
@File : test.py
@Author: gaojs
@Date: 2022/8/20 18:07
@Blogs : https://www.gaojs.com.cn
"""
from playwright.sync_api import Playwright, sync_playwright, expect
import playwright
import time
# Create the playwright object
playwright = sync_playwright().start()
# headless: true by default, headless mode # devtools is false by default: developer tools are off by default
# Browser object:, args=["--start-maximized"] Maximization does not take effect
browser = playwright.chromium.launch(headless=False, devtools=False)
# context manager object
context = browser.new_context(viewport={'width': 1920, 'height': 1080})
# page object
page1 = context.new_page()
page1.set_default_navigation_timeout(20000)
# open browser
page1.goto('https://www.gaojs.com.cn')
# Reload
page1.reload()
# Wait for an element on the page to appear
page1.wait_for_selector('//*[@id="Joe"]/header/div[1]/div/nav/a[7]')
# page1.locator().click()
# Click text=Image bed >> nth=0
with page1.expect_popup() as popup_info:
page1.locator("text=picture bed").first.click()
page2 = popup_info.value
page2.wait_for_url("http://image.gaojs.com.cn/explore/trending")
# Click text=Explore
page2.locator("text=Explore").click()
# d Wait for the search button to appear in the upper right corner of the page
page2.click("text=latest")
time.sleep(5)
# Close page
page1.close()
# Close page
page2.close()
# ---------------------
context.close()
browser.close()
5. Recording case (playback for a little debugging)
1. Automatically send blogs
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.gaojs.com.cn/admin/index.html
page.goto("https://www.gaojs.com.cn/admin/index.html")
# Go to https://www.gaojs.com.cn/admin/index.html#/
page.goto("https://www.gaojs.com.cn/admin/index.html#/")
# Go to https://www.gaojs.com.cn/admin/index.html#/login?redirect=%2Fdashboard
page.goto("https://www.gaojs.com.cn/admin/index.html#/login?redirect=%2Fdashboard")
# Click text=Shorthand Publish >> textarea
page.locator("text=Shorthand Post >> textarea").click()
# Fill text=Shorthand Post >> textareapage.locator("text=Shorthand Release >> textarea").fill("playwright automated advanced learning, Microsoft's new generation artifact!!!")
# Click button:has-text("Publish")
page.wait_for_url("https://www.gaojs.com.cn/admin/index.html#/posts/list?page=0&size=10&statuses=PUBLISHED&statuses=DRAFT&statuses=INTIMATE")
# Click span:nth-child(4)
page.locator("span:nth-child(4)").click()
# Click a:has-text("Profile")
page.wait_for_url("https://www.gaojs.com.cn/admin/index.html#/user/profile")
# Click div:has-text("system") >> nth=2
# Click a:has-text("Blog Settings")
page.wait_for_url("https://www.gaojs.com.cn/admin/index.html#/dashboard")
# Click span:nth-child(4)
page.locator("span:nth-child(4)").click()
# Click text=Sign out
page.locator("text=logout").click()
# Click button:has-text("OK")
page.wait_for_url("https://www.gaojs.com.cn/admin/index.html#/login?redirect=%2Fdashboard")
# Close page
page.close()
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
image-1660999546286
Related Articles
-
A detailed explanation of Hadoop core architecture HDFS
Knowledge Base Team
-
What Does IOT Mean
Knowledge Base Team
-
6 Optional Technologies for Data Storage
Knowledge Base Team
-
What Is Blockchain Technology
Knowledge Base Team
Explore More Special Offers
-
Short Message Service(SMS) & Mail Service
50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00