在 Playwright for Python 中,如何获取与 ElementHandle 相关的元素(children、parent、grandparent、兄弟姐妹)?
In Playwright for Python, how do I get elements relative to ElementHandle (children, parent, grandparent, siblings)?
在 playwright-python 我知道我可以使用 querySelector()
.
得到一个 elementHandle
示例(同步):
from playwright import sync_playwright
with sync_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = browser_type.launch()
page = browser.newPage()
page.goto('https://duckduckgo.com/')
element = page.querySelector('input[id=\"search_form_input_homepage\"]')
如何根据这个elementHandle
获取与this相关的元素? IE。 parent、祖父parent、兄弟姐妹、children 把手?
使用 querySelector()
/ querySelectorAll
XPath (XML Path Language) 允许您检索 elementHandle
(分别是句柄的集合)。一般来说,XPath 可用于浏览 XML 文档中的元素和属性。
from playwright import sync_playwright
with sync_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = browser_type.launch(headless=False)
page = browser.newPage()
page.goto('https://duckduckgo.com/')
element = page.querySelector('input[id=\"search_form_input_homepage\"]')
parent = element.querySelector('xpath=..')
grandparent = element.querySelector('xpath=../..')
siblings = element.querySelectorAll('xpath=following-sibling::*')
children = element.querySelectorAll('xpath=child::*')
browser.close()
在 playwright-python 我知道我可以使用 querySelector()
.
elementHandle
示例(同步):
from playwright import sync_playwright
with sync_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = browser_type.launch()
page = browser.newPage()
page.goto('https://duckduckgo.com/')
element = page.querySelector('input[id=\"search_form_input_homepage\"]')
如何根据这个elementHandle
获取与this相关的元素? IE。 parent、祖父parent、兄弟姐妹、children 把手?
使用 querySelector()
/ querySelectorAll
XPath (XML Path Language) 允许您检索 elementHandle
(分别是句柄的集合)。一般来说,XPath 可用于浏览 XML 文档中的元素和属性。
from playwright import sync_playwright
with sync_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = browser_type.launch(headless=False)
page = browser.newPage()
page.goto('https://duckduckgo.com/')
element = page.querySelector('input[id=\"search_form_input_homepage\"]')
parent = element.querySelector('xpath=..')
grandparent = element.querySelector('xpath=../..')
siblings = element.querySelectorAll('xpath=following-sibling::*')
children = element.querySelectorAll('xpath=child::*')
browser.close()