前言
我们在进行web自动化时,经常遇到一些不好操作的元素,普通的元素定位和操作容易报错,如果我们使用的selenium的话,就可以使用selenium调用js脚本进行操作。在playwright 中也有类似的方法,使用page.evaluate()执行JavaScript脚本。
page.evaluate()和page.evaluate_handle()之间的唯一区别是page.evaluate_handle()返回JSHandle。
- page.evaluate() 返回调用执行的结果
- page.evaluate_handle()返回JSHandle
page.evaluate()
返回evaluate() 返回执行JavaScript脚本的结果,使用示例如下:
from playwright.sync_api import sync_playwright
def run_js_script():
with sync_playwright() as playwright:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto('https://www.baidu.com')
print(page.evaluate("1 + 2"))
# 关闭浏览器
context.close()
browser.close()
run_js_script()
也可以执行一个函数,如下:
from playwright.sync_api import sync_playwright
def run_js_script():
with sync_playwright() as playwright:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto('https://www.baidu.com')
print(page.evaluate("() => '拜仁慕尼黑'"))
# 关闭浏览器
context.close()
browser.close()
run_js_script()
注:如果传递给page.evaluate()的函数返回一个不可序列化的值,则page.evaluate()解析为undefined
示例
- 打印网页标题
执行document.title 获取页面的title,代码如下:
from playwright.sync_api import sync_playwright
def run_js_script():
with sync_playwright() as playwright:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto("https://www.baidu.com/")
title = page.evaluate('document.title')
print(title)
page.pause()
# 关闭浏览器
context.close()
browser.close()
run_js_script()
- 操作元素示例
from playwright.sync_api import sync_playwright
def run_js_script():
with sync_playwright() as playwright:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto("https://www.baidu.com/")
js = """
document.getElementById('kw').value='playwright';
document.getElementById('su').click();
"""
page.evaluate(js)
page.pause()
# 关闭浏览器
context.close()
browser.close()
run_js_script()
页面如下:
page.evaluate_handle()
page.evaluate_handle()的返回值是JSHandle。
from playwright.sync_api import sync_playwright
def run_js_script():
with sync_playwright() as playwright:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto("https://www.baidu.com/")
a_handle = page.evaluate_handle("document.body")
result_handle = page.evaluate_handle("body => body.innerHTML", a_handle)
print(result_handle.json_value())
result_handle.dispose()
page.pause()
# 关闭浏览器
context.close()
browser.close()
run_js_script()
总结
本文主要介绍了playwright执行js脚本的操作,与selenium一样,playwright同样可以执行js脚本来完成一些不便操作的元素。
标签:playwright,浏览器,evaluate,JavaScript,sync,js,Playwright,context,page From: https://www.cnblogs.com/hogwarts/p/18191467