Playwright

from playwright.sync_api import sync_playwright
with sync_playwright() as self.playwright:

  1. 执行JS
# a_handle 创建一个js对象  "" 为js代码
a_handle = self.page.evaluate_handle("document")
self.page.evaluate_handle(
                "document.getElementsByClassName('index_shopName__Eocz8').click", a_handle)
  1. 保存cookie 持久化登录
# cookie_path - cookie文件保存路径
# storage_state - 加载cookie
# accept_downloads - 允许下载
cookie_path = os.path.join("文件路径", 'cookies-state.json')
self.browser = self.playwright.chromium.launch(headless=False)
if os.path.exists(cookie_path):	# 判断是否又找到cookie,有则加载
	self.context = self.browser.new_context(storage_state=cookie_path,
                                                            accept_downloads=True,)
else:	# 无则登录
	self.context = self.browser.new_context(accept_downloads=True)
self.page = self.context.new_page()		# 创建新的page
self.page.goto(login_url)				# 进入网址
self.wait(self.page)					# 等待加载
# 登录成功后需要保存cookies
self.context.storage_state(path=cookie_path)
  1. cdp方式登录
# 先手动打开浏览器并绑定本机随意端口
command = os.getcwd() + "\\ms-playwright\chromium-907428\chrome-win\chrome.exe --remote-debugging-port=9001"
print("浏览器路径:", command)
subprocess.Popen(command)
time.sleep(5)
# playwright 连接浏览器指定端口, 就可以连接上
self.chromium = playwright.chromium.connect_over_cdp(r"http://localhost:9001")
self.context = self.chromium.new_context(accept_downloads=True)
self.page = self.context.new_page()
self.page.goto(login_url)
self.wait(self.page)
  1. 一些方法
# 双击
self.page.dblclick('button:has-text(\"提交\")')

# 获取隐藏文本
context = self.page.query_selector('#ownerAddress_mf').get_property("value").json_value()

# 是否可见
self.page.is_visible('//*[@id="dlg-dialogList"]', timeout=5000)

# 重新刷新加载页面
self.page.reload()

# 找到所有的iframe
self.page.frames

# 等待方式有几种,domcontentloaded,networkidle
self.page.wait_for_load_state('domcontentloaded')

# 选择下拉框	- 根据下拉框元素选择“是”
self.page.select_option('#sendCommTypeCode', "是")

# 加载新page,获取新page信息
self.page.frames	# 方法一
# 方法二
with self.page.expect_popup() as popup_info:
    self.page.click('text="跳转"')
new_page = popup_info.value		# 新page信息
new_page.reload()  # 重新加载
# 第三种方式获取新page信息
new_page = self.get_newPage(self.page,'text="跳转"')
# 注意事项:有时候刷新不出元素信息,先随便找个元素激活下
self.new_page.query_selector('.ui_dialog')  # 随便找个元素激活下

# 获取网页返回数据
with self.page.expect_response("请求的url") as second:
	self.page.click('span:text("搜 索")')
	time.sleep(1) # 等待一下,self.wait(self.page)
print(second.value.json())

# 上传文件
with self.page.expect_file_chooser() as fc_info:
    # 点击上传 - 必须要这个触发
    self.page.click('//*[@id="file-1241-button"]')
file_chooser = fc_info.value		# 上传事件信息
file_chooser.set_files("文件路径")	# 添加文件

# 下载文件
# 允许下载设置为True  --  accept_downloads=True
self.context = self.chromium.new_context(accept_downloads=True)
# 获取下载信息重新另存 (原本的文件名:download.suggested_filename)
with self.page.expect_download() as download_info:
	# 点击下载 - 必须要这个触发
    self.page.click('.oui-download-text-info')
download = download_info.value	# 下载事件信息
download.save_as("文件保存路径")		# 文件另存为

# 监听弹窗
	# 获取弹出框内容
	def handle_dialog(self,dialog):
	    print(dialog.message)
	    self.alert_info = dialog.message
	    dialog.accept()
	# 在需要的地方进行监听
	self.page.on("dialog", self.handle_dialog)
  1. 键盘操作
# 键盘操作
self.page.keyboard.press('Control+V')				# 快捷键无需找到元素
self.page.press('#consigneeCountryName', "Enter")	# 快捷键需要找到元素
self.page.keyboard.type('文本内容')					# 按键输入文本 无需找到元素
self.page.type('文本内容')							# 按键输入文本 需要找到元素
  1. 拓展
# 复制到剪切板
import pyperclip
pyperclip.copy("文本内容")

其他方案
https://playwright.dev/docs/intro/ // 官方
https://www.yisu.com/zixun/620215.html // 博客
https://blog.51cto.com/xqtesting/2677875 // 元素定位


版权声明:本文为weixin_43407806原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。