playwright

举报
建帅小伙儿 发表于 2022/09/25 00:46:37 2022/09/25
【摘要】 一、page_func.py # coding=utf-8 """ @Project :playwright_test @File :page_func.py @Author :gaojs @Date :2022/7/14 21:48 @Blogs : https:/...

一、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()

文章来源: blog.csdn.net,作者:懿曲折扇情,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_41332844/article/details/126837309

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。