目 录CONTENT

文章目录

playwright

懿曲折扇情
2022-07-14 / 1 评论 / 8 点赞 / 102 阅读 / 638 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-07-14,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
广告 广告

一、page_func.py

# coding=utf-8
"""
    @Project :playwright_test 
    @File    :page_func.py
    @Author  :gaojs
    @Date    :2022/7/14 21:48
    @Blogs   : https://www.gaojs.com.cn
"""
import time

import playwright
from playwright.sync_api import Playwright, sync_playwright, expect
from playwright.sync_api import sync_playwright


class AMPTest:
    """
    amp类
    """
    def __init__(self):
        """
        初始化参数
        """
        self.playwright = sync_playwright().start()
        self.browser = self.playwright.chromium.launch(headless=False)
        self.context = self.browser.new_context()
        self.page = self.context.new_page()
        self.page.set_default_navigation_timeout(20000)

    async def async_browser(self):
        """
        异步打开多个浏览器
        :return:
        """
        async with async_playwright() as p:
            for browser_type in [p.chromium, p.firefox, p.webkit]:
                browser = await browser_type.launch(headless=False)
                page = await browser.new_page()
                await page.goto('www.baidu.com')
                # await browser.close()
    asyncio.get_event_loop().run_until_complete(async_browser())
    
    def gotourl(self, url):
        """
        打开新页面
        :return:
        """
        self.page.goto(url=url)
        self.page.wait_for_load_state(timeout=10)

    def wait_element_click(self, locator):
        """
        等待点击
        :return:
        """
        try:
            self.page.locator(locator).click()
            self.page.wait_for_load_state()
        except Exception as err:
            self.page.screenshot(path="./a.png")
            print(f'点击元素失败,请重试!错误信息是 {err}')

    def wait_element_input(self, locator, text):
        """
        等待元素加载出来,输入text
        :return:
        """
        try:
            self.page.locator(locator).fill(text)
            self.page.wait_for_load_state()
        except Exception as err:
            self.page.screenshot(path="./123.png")
            print(f'定位元素失败失败,请重新输入!错误信息是 {err}')

    def refresh(self):
        """
        刷新页面
        :return:
        """
        self.refresh()
        self.page.wait_for_load_state()

    def reload(self):
        """
        重新加载页面
        :return:
        """
        self.page.reload(timeout=20)
        self.page.wait_for_load_state()

    def closeCurrent(self):
        """
        关闭当前页面
        :return:
        """
        self.page.close()

    def title(self):
        """
        等待title出现
        :return:
        """
        self.page.title()

    def doubleClick(self, locator):
        """
        双击
        :return:
        """
        self.page.locator(locator).dblclick()

    def selectFrame(self, name):
        """
        切换frame
        :return:
        """
        self.page.frame(name=name)

    def waitConsoleMessage(self, predicate):
        """
        等待界面出现
        :return:
        """
        self.page.expect_console_message(predicate)

    def login(self):
        """
        登录amp站点
        """

        self.page.goto('https://www.gaojs.com.cn')
        # d等待页面右上角搜索按钮出现
        self.wait_element_click('#Joe > header > div.joe_header__above.topInDown.fixed > div > nav > a:nth-child(2)')
        time.sleep(5)
        page2 = self.context.new_page()
        page2.goto('https://www.gaojs.com.cn/s/about')
        time.sleep(5)
        page2.locator('#Joe > header > div.joe_header__above.topInDown.fixed > div > form > input').fill('自动定时发送')
        page2.locator('//button[@aria-label="搜索按钮"]/i').click()
        time.sleep(5)
        self.page.close()
        page2.close()


test = AMPTest()
test.login()

8

评论区