I have already read the documentation but I'm still kinda confused about get_by_text
method in playwright python:
I have a small code demonstration that I try to get the selector of the text Sr. Enterprise Cloud & Snowflake Architect
then I just want to know the nature of that selector, is it a div or span or etc ..
from playwright.sync_api import sync_playwright
import os
my_path = os.getcwd()
html = open("l.html", "r", encoding="utf-8").read()
with sync_playwright() as p:
chromium_path = f'{my_path}/chromium-1076/chrome-win/chrome.exe'
browser = p.chromium.launch(executable_path=chromium_path, headless=False)
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)
browser.close()
the issue that I'm getting is this strict mode violation that I don't get it to be honest :
playwright._impl._api_types.Error: Error: strict mode violation: get_by_text("Sr. Enterprise Cloud & Snowflake Architect", exact=True) resolved to 2 elements:
1) <span aria-hidden="true">…</span> aka get_by_text("Sr. Enterprise Cloud & Snowflake Architect").first
2) <span class="visually-hidden">…</span> aka get_by_text("Sr. Enterprise Cloud & Snowflake Architect").nth(1)
=========================== logs ===========================
waiting for get_by_text("Sr. Enterprise Cloud & Snowflake Architect", exact=True)
What I want to know is that get_by_text()
method really return a locator of the matched text or no, and that issue how to solve it if possible
page.get_by_text()
returns a locator, similar to page.locator()
(and many other methods) but with a text query.
Locators are a declarative way to select elements. Simply calling page.get_by_text("selector")
does nothing--it's merely a declaration of intent. So calling the locator variable element
is a bit misleading because no element has been retrieved yet.
Later on, once you run element.evaluate()
, then the locator springs into action. Playwright runs a selection for the locator to find the elements that match it, automatically waiting until existence and actionability if necessary, and performs the evaluation in the browser context.
The next concept at play here is strict mode. By default, Playwright throws an error if you try to perform a singleton action when the locator resolves to multiple elements. The error is a safety measure to avoid a situation where a new element is introduced onto the page, causing your previous selection to switch silently to an unintended target. The error prompts you to be more specific in your locator query and offers code to disambiguate the multiple matches.
You can disable strict mode and always select the first element, but this is discouraged. I'd look at l.html
and disambiguate based on the broader context of what you're trying to accomplish and the elements' available attributes, parent tree and subtree. There's no one-size-fits-all way to disambiguate elements.
By the way, it's a bit unusual to want to pull the tagName
from an element. 99% of the time, there's no need to do this, so either this is a stub for some other action, or you may be pursuing an antipattern. You might want to provide some high-level context for what you're trying to do.