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

[Question] Waiting for a selector with a specific index #4995

Closed
15alja opened this issue Jan 13, 2021 · 6 comments
Closed

[Question] Waiting for a selector with a specific index #4995

15alja opened this issue Jan 13, 2021 · 6 comments

Comments

@15alja
Copy link

15alja commented Jan 13, 2021

Given a page with the following code

<div>
  <span>span1</span>
</div>
<div>
  <span>span2</span>
</div>

What is the best way to access the 2nd span element with Playwright? The css selector nth-of-type is not what I'm looking for - I just want the 2nd instance of span in a generic way.

@yury-s
Copy link
Member

yury-s commented Jan 13, 2021

The following snippet should get it:

const span = (await page.$$('span'))[1];

@yury-s yury-s closed this as completed Jan 13, 2021
@15alja
Copy link
Author

15alja commented Jan 14, 2021

The following snippet should get it:

const span = (await page.$$('span'))[1];

Thanks, however this is not precisely what I'm looking for. I want to wait for this element with index 1 to appear, similar to waitForSelector.

@yury-s
Copy link
Member

yury-s commented Jan 14, 2021

Why not use waitForSelector then, can you tell more about your constraints ?

@15alja
Copy link
Author

15alja commented Jan 14, 2021

waitForSelector waits for the first element matching a selector. Specifically, I want to wait for the k-th element matching a selector.

I snooped through some PRs and found that what I'm requesting was removed in #4640, though I'm unsure why.

@dgozman
Copy link
Contributor

dgozman commented Jan 14, 2021

We are working on the index selector, see issue #3697.

@15alja
Copy link
Author

15alja commented Jan 18, 2021

@dgozman great, I'm excited to see that this is being worked on

For now, I accomplish it by continuously querying until it is found or timing out. It's a hacky approach that I would prefer to avoid, but do I have any better options for now?

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

3 participants