Elements has a "lazy" interface (as it was in protractor). The collection is not resolved until an action is performed on it.
All examples work identically with both playwrightWD and seleniumWD.
- Selector strategies
- get
- first
- last
- count
- getFirstVisible
- getAllVisible
- each
- map
- some
- every
- find
- filter
- getEngineElements
const { playwrightWD } = require('promod');
const { $$ } = playwrightWD;
const byCss = $$('.class #id div a[href*="link"]');
const byXpath = $$('xpath=.//div[@data-test="id"]/span');
const byJS = $$(() => document.querySelectorAll('div > span'));
const byCustom = $$({ query: 'button', text: 'Submit' });Returns a single element from the collection by index. Negative indexes count from the end.
const buttons = $$('button');
const first = buttons.get(0);
const third = buttons.get(2);
const last = buttons.get(-1);
await first.click();Shorthand for get(0).
const button = $$('button').first();
await button.click();Shorthand for get(-1).
const button = $$('button').last();
await button.click();Returns the number of elements matching the selector.
const count = await $$('button').count();Returns the first visible (displayed) element in the collection.
const visibleButton = $$('button').getFirstVisible();
await visibleButton.click();Returns a new elements collection containing only visible elements.
const visibleButtons = $$('button').getAllVisible();
const visibleCount = await visibleButtons.count();Iterates over each element in the collection.
const buttons = $$('button');
await buttons.each(async (button, index) => {
await button.click();
});Maps each element to a value.
const buttons = $$('button');
const texts = await buttons.map(async (button) => {
return await button.getText();
});Returns true if the callback returns true for at least one element.
const buttons = $$('button');
const isSomeVisible = await buttons.some(async (button) => {
return await button.isDisplayed();
});Returns true if the callback returns true for every element.
const buttons = $$('button');
const allVisible = await buttons.every(async (button) => {
return await button.isDisplayed();
});Finds the first element matching the callback condition.
const buttons = $$('button');
const submitBtn = await buttons.find(async (button) => {
return (await button.getText()) === 'Submit';
});
await submitBtn.click();Returns a new elements collection with elements matching the callback condition.
const buttons = $$('button');
const enabledButtons = buttons.filter(async (button) => {
return await button.isEnabled();
});
const count = await enabledButtons.count();Returns the underlying native engine elements (Playwright Locator[] / Selenium WebElement[]).
const nativeElements = await $$('button').getEngineElements();