playwright 应用

侠客云 / 2023-08-03 / 原文

一 安装

pip install playwright 
pip install pytest-playwright
playwright install  #安装chouim

小案例1:

from playwright.sync_api import sync_playwright
with sync_playwright() as p:
    brower =p.chromium.launch(headless=False,args=["--start-maxmized"],slow_mo=3000)
    page =brower.new_page(no_viewport=True) # 默认窗口大小失效
    # page =brower.new_page(viewport={"width":1920,"height":1080}) # 默认窗口大小失效
    page.goto("https://www.baidu.com")
    print(page.title())
    page.get_by_text("百度一下").click() # 根据文本找到
    page.go_back() # 后退
    page.go_forward()# 前进
    page.reload() # 刷新
    brower.close()

小案例2:

from playwright.sync_api import sync_playwright
playwright = sync_playwright().start()
browser = playwright.chromium.launch(headless=False,slow_mo=2000)
context = browser.new_context()
page = context.new_page()
page.goto("https://www.baidu.com")
# $x()
# xpath定位
#单属性定位
text1= page.locator("//span[@name='tj_settingicon']").text_content()
print(text1)
# 多属性定位 and
# css定位
page.locator("#kw").fill("python")
page.locator("#su").click()
page.locator("#kw").fill("java")
page.locator("input#kw").fill("java") # 标签加属性的方式
page.locator("#su").click()
page.fill("//input[@id='kw']","你好啊")
page.click("//input[@id='su']")
browser.close()

 截图:

#方式 1
page.screenshot(path="./1.png") # 长截图 self.page.screenshot(path="screenshot.png", full_page=True) # 截取单个元素 locator-定位器 page.locator("//input[@placeholder='手机号']").screenshot(path="./1.png")

 小案例3:

from playwright.sync_api import sync_playwright
import time

class XiaoHongShu:
    def __init__(self):
        playwright = sync_playwright().start()
        self.browser = playwright.chromium.launch(headless=False)
        self.context = self.browser.new_context() # 第1个chromium
        # self.context2 = self.browser.new_context() # 第2个chromium
        self.page = self.context.new_page() # tab页
        # self.page2 = self.context2.new_page() # tab页
    def run(self):
        # page2 = context.new_page()
        # page2.goto("https://www.bilibili.com/")
        # self.page2.goto("https://www.bilibili.com/")
        self.page.goto("https://creator.xiaohongshu.com/login")
        self.page.fill("//input[@placeholder='手机号']","18179642779")
        self.page.click("//div[text()='发送验证码']")
        self.page.fill("//input[@placeholder='验证码']","123456")
        self.page.click("//span[text()=' 登 录 ']")
        # self.page.screenshot(path="./1.png")
        # 长截图
        # self.page.screenshot(path="screenshot.png", full_page=True)
        # 截取单个元素 locator-定位器
        # self.page.locator("//input[@placeholder='手机号']").screenshot(path="./1.png")

        #  上传文件 切换到某个iframe,然后找到上传文件元素
        # self.page.frame_locator("//ifrmage1").locator("//span[@class='']").set_input_files("./1.txt")
        # 选择下拉框
        # self.page.select_option("//*[@id='twon']",value="25609")
        # self.page.select_option("//*[@id='twon']",label="湖南省")

        # self.page.query_selector_all("")
        # 悬停
        # self.page.hover()

        # 创建新的页面
        with self.context.expect_page() as new_page_info:
            #打开新的tab页
            self.page.click("a[target='_blank']")
        new_page = new_page_info.value
        new_page.wait_for_load_state()
        print(new_page.title())
        # page.close()
        # page2.close()
        time.sleep(6)
        # self.browser.close()

if __name__ == '__main__':
    xhs = XiaoHongShu()
    xhs.run()