一個頁面可以附加一個或多個?Frame?對象。每個頁面都有一個主框架,并且假定頁面級交互(如)在主框架中運行。click
frame_locator
使用 iframe 時,可以創建一個框架定位器,該定位器將進入 iframe 并允許選擇該 iframe 中的元素。
# Locate element inside frame
# Get frame using any other selector
username = page.frame_locator('.frame-class').get_by_label('User Name')
username.fill('John')
上面代碼,先定位frame,然后定位frame里的元素,并對元素進行輸入操作。
示例:
html文件
<!DOCTYPE html>
<html>
<head><title>IFrame Example</title>
</head>
<body><h1>IFrame Example</h1><p>This is an example of using an iframe.</p><iframe src="https://pity.fun/#/dashboard/workspace" frameborder="0" width="100%" height="500"></iframe>
</body>
</html>
這個html頁面有一個iframe
from playwright.sync_api import sync_playwright, expectdef run(playwright):chromium = playwright.chromiumbrowser = chromium.launch(headless=False)page = browser.new_page()page.goto(r'C:\Users\Desktop\download.html')page.frame_locator("iframe").get_by_placeholder("用戶名: tester").fill("tester")page.frame_locator("iframe").get_by_placeholder("密碼: tester").fill("tester")page.frame_locator("iframe").get_by_role("button", name="登 錄").click()expect( page.frame_locator("iframe").get_by_title("工作臺")).to_be_visible()with sync_playwright() as p:run(p)
如果要對iframe里元素進行操作,就需要先用frame_locator來定位iframe,再定位iframe里的元素。
?frame
可以使用?page.frame()?返回匹配的frame。
需要指定frame的name或者url。
# Get frame using the frame's name attribute
frame = page.frame('frame-login')# Get frame using frame's URL
frame = page.frame(url=r'.*domain.*')# Interact with the frame
frame.fill('#username-input', 'John')
frame tree
頁面都通過page.main_frame和frame.child_frames方法公開其當前框架樹。
框架對象的生命周期由三個事件控制,這些事件在頁面對象上調度:
- page.on(“frameattached”)?- 當框架附加到頁面時觸發。一個框架只能附加到頁面一次。
- page.on(“framenavigated”)?- 當框架提交導航到其他 URL 時觸發。
- page.on(“framedetached”)?- 當框架從頁面分離時觸發。一個框架只能從頁面分離一次。
from playwright.sync_api import sync_playwrightdef run(playwright):firefox = playwright.firefoxbrowser = firefox.launch()page = browser.new_page()page.goto("https://www.theverge.com")dump_frame_tree(page.main_frame, "")browser.close()def dump_frame_tree(frame, indent):print(indent + frame.name + '@' + frame.url)for child in frame.child_frames:dump_frame_tree(child, indent + " ")with sync_playwright() as playwright:run(playwright)
add_script_tag
?
frame.add_script_tag()
?方法在頁面中添加一個腳本標簽:
下面是一個例子,演示如何使用 playwright 的 `frame.add_script_tag()` 方法在頁面中添加一個腳本標簽:
from playwright.sync_api import Playwright, sync_playwrightwith sync_playwright() as p:browser = p.chromium.launch()page = browser.new_page()page.goto('https://www.example.com')# 在頁面中添加一個腳本標簽page.main_frame().add_script_tag(url='https://code.jquery.com/jquery-3.6.0.min.js')# 在頁面中執行添加的腳本page.evaluate('console.log(jQuery.fn.jquery)')browser.close()
在上面的代碼中,首先創建了一個 `browser` 實例和一個 `page` 實例,并跳轉到指定的網址。然后使用 `page.main_frame().add_script_tag()` 方法添加了一個 jQuery 的腳本標簽,這個腳本標簽的 URL 是 `https://code.jquery.com/jquery-3.6.0.min.js`。最后使用 `page.evaluate()` 方法在頁面上執行添加的腳本,輸出了 jQuery 的版本號。
add_style_tag
在樣式表的加載觸發或將 CSS 內容注入框架時返回添加的標記。
將具有所需 URL 的標記或包含內容的標記添加到頁面中。<link rel="stylesheet">
<style type="text/css">
用法
frame.add_style_tag()
frame.add_style_tag(**kwargs)
content
獲取框架的完整 HTML 內容,包括文檔類型。
frame.content()
drag_and_drop
frame.drag_and_drop(source, target)
frame.drag_and_drop(source, target, **kwargs)