Skip to content
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

Playwright でページに複数個の要素が存在することを検証する #3599

Open
YumaInaura opened this issue Apr 18, 2024 · 0 comments

Comments

@YumaInaura
Copy link
Owner

YumaInaura commented Apr 18, 2024

HTML

HTMLにはこんな風にテキストが10個あるとする

<div>FOO TEXT</div>
<div>FOO TEXT</div>
<div>FOO TEXT</div>
<div>FOO TEXT</div>
<div>FOO TEXT</div>
<div>FOO TEXT</div>
<div>FOO TEXT</div>
<div>FOO TEXT</div>
<div>FOO TEXT</div>
<div>FOO TEXT</div>

Playwright

専用の書き方があるわけではなく、ループで回せば良いようだ

画面に FOO TEXT というテキストが10個あることを検証する例

for (let i = 0; i < 10; i++) {
  await expect(page.getByText('FOO TEXT').nth(i)).toBeVisible()
}

nth

なお Playwright の nth では指定順番の要素を得られるようだが、 0始まりのカウントのようだ

  • nth(0) が1番目の要素
  • nth(1) が1番目の要素

Returns locator to the n-th matching element. It's zero based, nth(0) selects the first element.

https://playwright.dev/docs/api/class-locator

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

プロフィール・経歴

https://github.com/YumaInaura/YumaInaura

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant