-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Docs update for text= vs :has-text vs :text #5748
Comments
text=
vs has-text
text=
vs :has-text
vs :text
text=
vs :has-text
vs :text
If These three are very similar indeed and the difference is subtle. All of them will perform case-insensitive match of the text regardless of its dom structure, i.e. the text could be spread across several nodes. await page.setContent(`<div id=nav-bar>Hello<i>,</i> <span id=nav-name>Playwright</span><b>!</b></div>`);
expect(await page.$eval(`#nav-bar >> text=Playwright`, e => e.id)).toBe('nav-name');
expect(await page.$eval(`#nav-bar:has-text("Playwright")`, e => e.id)).toBe('nav-bar');
expect(await page.$eval(`#nav-bar :text("Playwright")`, e => e.id)).toBe('nav-name'); You are right that we should add more examples to highlight differences between different selector types. |
Thanks for the clarification! A doc update to let the reader know this would be good. It seems that the first and last example are functionally equivalent, but that Aside: To me the second example with |
"it" in this case instead being But to me that would mean it matches the outer-most element of the hold web page, at least, that is how I read it. I.e. it should always match |
No, by "it" I meant #nav-bar, the selector would have to be |
Got it, thanks! |
I just read https://playwright.dev/docs/selectors/#text-selector
There are a few ways to look for text on a page, but I cannot from the docs deduct which one I should use. For example these three:
To me, these seem functionally equivalent from the docs. Is that assumption incorrect?
Also, which one is the Best practice to use out of these? I've always used
>> text
before the other psuedo classes became available, but I am not sure that if I refactor my code which of these I should use.>> text
has looks cleaner to me, easier to read, and is faster to type.The text was updated successfully, but these errors were encountered: