question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] How to locate parent element with exact child text without using RegExp?

See original GitHub issue

Hi, I have problem with locating element containg text. What is important, I don’t want to search for child element, but I would like to find div, which one of the child element contains defined text. Let’s assume I have 2 elements: <div><label>Playwright</label></div> <div><label>Playwright is cool</label></div>

I would like to find element div with exact text “Playwright”. When looking for: playwright.locator(‘div >> text=“Playwright”’) -> it found label When looking for: playwright.locator(‘div:has-text(“Playwright”)’) -> it found div, but both 2 elements 😦

I know I can look for: playwright.locator(‘div:text-matches(“Playwright”,“gm”)’), but I would like not to use RegExp (if possible)

Is there any possibility I could search for this element without using RegExp? Or maybe is there a locator allowing me to look for parent? Then I could found <label>Playwright</label>, and based on this find parent(div)?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
dgozmancommented, Apr 4, 2022

Try the following snippet. Note the double quotes in the text= selector - these insist on the strict match. Learn more in this guide.

page.locator('div', { has: page.locator('text="Playwright"') })
0reactions
mxschmittcommented, Apr 14, 2022

Closing as per above since the main issue is answered.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Complex CSS selector for parent of active child - Stack Overflow
My question is whether or not it is possible to apply a style to the list item that contains the anchor with the...
Read more >
Selectors - W3C
The selector matches if E1 and E2 share the same parent in the document tree and E1 immediately precedes E2, ignoring non-element nodes...
Read more >
Using Parent / Child Relationships — ciscoconfparse 1.6.40 ...
CiscoConfParse() uses these parent-child relationships to build queries. For instance, you can find a list of all parents with or without a child;...
Read more >
Selectors — Scrapy 2.7.1 documentation
Selectors¶. When you're scraping web pages, the most common task you need to perform is to extract data from the HTML source.
Read more >
How To Find HTML Elements Using Cypress Locators
A better approach would be to use the regex-supported syntax in the CSS selector to get the list of items in one go....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found