I want to get the tagname tagName of element selector selected by get_by_text()
method
for example :
element = page.get_by_text("example", exact=True)
This is a rather unusual request, so there may be a better way to solve whatever fundamental problem you're trying to solve, but you can extract a tag from an element as follows:
element = page.get_by_text("example", exact=True)
tag_name = element.evaluate("el => el.tagName")
Complete, runnable example:
from playwright.sync_api import sync_playwright,expect # 1.43.0
html = '<span aria-hidden="true"><!---->Sr. Enterprise Cloud & Snowflake Architect<!----></span>'
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.set_content(html)
element = page.get_by_text("Sr. Enterprise Cloud & Snowflake Architect", exact=True)
tag_name = element.evaluate("el => el.tagName")
print(tag_name) # => SPAN
browser.close()
If you're doing testing and anticipate a certain tag to be present, try:
expect(page.locator("p").locator("text='example'")).to_be_visible()
or without expect
:
page.locator("p").locator("text='example'").wait_for()
Generally speaking, in testing, you don't want to care much about tag names since they're not user visible.