page.locator(`text="foobar"`)だと取れなかったところ

version情報

"@playwright/test": "1.22.2"

起こったこと

こういったインライン要素が入った文字を完全一致で取得できなかった。

<p id="foobar">foo<strong>bar</strong></p>
await page.click(`text="foobar"`)
/**
 *Timeout of 30000ms exceeded.

  page.click: Target closed
  =========================== logs ===========================
  waiting for selector "text="foobar""
  ============================================================
  */

ここに書いてあった

playwright.dev

text="Log in" - text body can be escaped with single or double quotes to search for a text node with exact content. For example, does not match because contains a single text node that is not equal to . However, matches , because contains a text node . This exact mode implies case-sensitive matching, so will not match .text="Log"

でも、完全一致が使いたい

正規表現が使えるのでこれをやったら動いた。

await page.click(`text=/^foobar$/`)