在 playwright-python 我知道我可以得到一个 elementHandle
使用 querySelector()
.
示例(同步):
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
? IE。 parent ,祖 parent , sibling , 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()
关于python - 在 Python 版剧作家中,如何获取与 ElementHandle 相关的元素( child 、 parent 、祖 parent 、 sibling )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65400809/