Skip to content

[Feature] selector improvements proposals #2370

@dgozman

Description

@dgozman

waitForSelectors

There are quite a few requests for more advanced logic to retrieve elements matching selectors. For example:

  • wait for at least one element matching selector and get all matching elements;
  • wait for at least one element matching selector to be visible and get it.

Proposal:

page.waitForSelectors(selector, countOrPredicate, { state?: 'attached' | 'visible' | 'hidden', filter? }): Promise<ElementHandle[]>

This method will query all elements matching selector, filter them by them by visibility state, then filter by the filter predicate, and then resolve with remaining elements if there are not less than countOrPredicate of them when it's a number or the predicate returns true.

Examples:

  • page.waitForSelectors('button', 2) - at least two buttons.
  • page.waitForSelectors('button', buttons => buttons.length >= myButtonsCount) - at least myButtonsCount buttons;
  • page.waitForSelectors('button', 1, { state: 'visible' }) - at least one visible button;
  • page.waitForSelectors('button', 3, { state: 'visible', filter: b => !b.disabled }) - at least three visible and enabled buttons.

selectors.visible

There are requests to match not the first element, but the first visible one. This is more flaky and the preferred way is to use a stable selector the uniquely identifies the visible target element. That said, maybe it makes sense to introduce a wrapper that takes a regular selector:

selectors.visible('css=div')

Questions:

  • How does this interact with waitForSelector() state option that could be visible | hidden | attached | detached?

selectors.index

This could simplify selecting a particular item from the list.

Examples:

  • page.click(selectors.index('.my-list-item', 3)) - click at the third list item;
  • page.waitForSelector(selectors.index('button', 2)) - wait until there are at least two buttons.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions