576i
576i

Reputation: 8362

In Playwright for Python, how do I use a QuerySelector to match partial text inside a tag?

I am trying to match a H1 tag on a page that has several h1 tags using partial text.

For this example the text is "Your booking was"

<header data-v-20d51c72="" data-v-3cdc5826="" class="default__title">
   <h1 data-v-20d51c72="" data-v-3cdc5826="" class="default__title">Your booking was unsuccessful.</h1>
</header>

I can locate against the full text like this

 page.querySelector('//h1[normalize-space()="Your booking was unsuccessful."]')

I can also get a list of all h1 tags and then check the innerHTML for all

 [handle for handle in page.querySelectorAll('//h1') if handle.innerHTML().startswith('Your booking was')]

But this way, I can't wait for the handle to appear.

How can I write a selector so that I can match against a partial tag so that I can use page.WaitForSelector() on it?

Upvotes: 1

Views: 6102

Answers (1)

576i
576i

Reputation: 8362

Taking a hint from this SO question a working selector is

handle = page.querySelector('//h1[contains(., "Your booking was")]')

Where

print(handle.innerHTML())

prints 'Your booking was unsuccessful.'

Upvotes: 3

Related Questions