最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I use page.waitForSelector in Playwright for one of two selectors? - Stack Overflow

programmeradmin4浏览0评论

I am trying to await a div that has one of two possible ids, div#abc or div#efg.

I can await div#abc like this:

    await this.page.waitForSelector('div#abc');

But how can I tell Playwright that I also want the step to pass if div#efg is present?

I am trying to await a div that has one of two possible ids, div#abc or div#efg.

I can await div#abc like this:

    await this.page.waitForSelector('div#abc');

But how can I tell Playwright that I also want the step to pass if div#efg is present?

Share Improve this question edited Jul 17, 2024 at 13:55 ggorlen 56.9k8 gold badges110 silver badges150 bronze badges asked Mar 24, 2022 at 3:48 Patrick KennyPatrick Kenny 6,39718 gold badges62 silver badges102 bronze badges 2
  • See this - stackoverflow.com/a/77128725 – Vishal Aggarwal Commented Feb 29, 2024 at 12:27
  • In the above linked post playwright Or operator is discussed in detail which is native playwright solution to this problem, – Vishal Aggarwal Commented Feb 29, 2024 at 12:37
Add a comment  | 

3 Answers 3

Reset to default 11

Like with Puppeteer, Playwright selectors can be standard CSS, so just add a comma between the selectors:

await this.page.waitForSelector('div#abc, div#efg');

More general solution that works for any number of async operations:

const firstResolvedPromiseResult = await Promise.race([
  this.page.waitForSelector('div#abc'),
  this.page.waitForSelector('div#efg'),
]);

But the accepted solution is simpler for this use case.

Simply use playwright native Locator OR:

await this.page.waitForSelector(locatorA.or(locatorB))

Reference: https://stackoverflow.com/a/77288539/1831456

发布评论

评论列表(0)

  1. 暂无评论