playwright系列文章目录
00Mac安装playwright
00Mac 安装配置Python3虚拟环境(VirtualEnv、virtualenvwrapper扩展包)
01【python+playwright测试自动化】之定位方式
文章目录
前言
本文只提供简单的网易邮箱登录、发送自动化实现,后面会更新优化之后的代码,可以作为对比学习。
一、实现登录邮箱初版
# encoding: utf-8
# @File : test.py
# @Author: 佳佳不爱上班
# @Date : 2024/09/10
import re
import time
from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto('https://mail.163.com/')
frame = page.frame_locator('//html//body//div[3]//div[3]//div[1]//div//div[3]//div[1]//div[2]//iframe')
frame.get_by_placeholder('邮箱账号或手机号码').fill('[email protected]')
# frame.get_by_placeholder("输入密码").fill('xxxx')
frame.locator('#pwdtext').fill('Aa123456')
time.sleep(2)
frame.get_by_label('30天内免登录').click()
frame.locator('#dologin').click()
# 写信
page.get_by_role('button', name='写 信').click()
time.sleep(2)
# 收件人
page.locator('.nui-editableAddr-ipt').fill('[email protected]')
time.sleep(2)
# 主题
page.locator('//html/body//div[2]//div[1]//div[2]//div[1]//section//header//div[2]//label//span').fill('测试')
time.sleep(2)
# 输入正文
context_frame = page.frame_locator('.APP-editor-iframe')
context_frame.locator('.nui-scroll').fill('正文内容')
time.sleep(2)
# 发送
page.locator('//html//body//div[2]//div[1]//div[2]//header//div//div[1]//div//span[2]').click()
time.sleep(2)
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
总结
根据上一篇介绍的元素定位方式,去尝试自己定位元素,实现简单的网易邮箱登录、发送邮件功能,主要为了加深对元素定位的理解、应用。
标签:02,playwright,python,frame,locator,time,div,page From: https://blog.csdn.net/m0_55605424/article/details/142108465