在Playwright for Python中,如何获取与ElementHandle相关的元素(子元素、父元素、祖元素、兄弟元素)?

klh5stk1  于 12个月前  发布在  Python
关注(0)|答案(3)|浏览(166)

在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获取与此相关的an元素?即parent、grandparent、siblings、children句柄?

wfsdck30

wfsdck301#

原始答案:

使用querySelector()/querySelectorAllXPath (XML Path Language)可以检索elementHandle(分别是句柄的集合)。一般来说,**可以用于在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()

字符串

更新(2022-07-22):

似乎browser.newPage()已被弃用,因此在较新版本的playwright中,该函数被称为browser.new_page()(注意不同的函数名称)。
可以选择先创建一个浏览器上下文(然后关闭它),并在该上下文上调用new_page()
访问孩子/父母/祖父母/兄弟姐妹的方式保持不变。

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)
        context = browser.new_context()
        page = context.new_page()
        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::*')

        context.close()
        browser.close()

svdrlsy4

svdrlsy42#

接受的答案是在旧版本的剧作家。使用以下格式为当前版本,它将工作。

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    for browser_type in [p.chromium, p.firefox, p.webkit]:
        browser = browser_type.launch(headless=False)
        context = browser.new_context()
        page =context.new_page()
        page.goto('https://duckduckgo.com/')
        element = page.query_selector('input[id=\"search_form_input_homepage\"]')
        
        parent = element.query_selector('xpath=..')
        grandparent = element.query_selector('xpath=../..')
        siblings = element.query_selector_all('xpath=following-sibling::*')
        children = element.query_selector_all('xpath=child::*')

        context.close()
        browser.close()

字符串

l2osamch

l2osamch3#

现在不再使用query_selector,而是使用

child_element = page.locator('input[id=\"search_form_input_homepage\"]')
parent = page.locator('div').filter(has=child_element)
grandparent = page.locator('xpath=../..')
siblings = page.locator('xpath=following-sibling::*')
children = page.locator('xpath=child::*')

字符串
参见链接

相关问题