sidebar_label |
---|
ElementHandle.waitForSelector |
Wait for an element matching the given selector to appear in the current element.
Unlike Frame.waitForSelector(), this method does not work across navigations or if the element is detached from DOM.
class ElementHandle {
waitForSelector<Selector extends string>(
selector: Selector,
options?: WaitForSelectorOptions
): Promise<ElementHandle<NodeFor<Selector>> | null>;
}
Parameter |
Type |
Description |
---|---|---|
selector |
Selector |
The selector to query and wait for. |
options |
(Optional) Options for customizing waiting behavior. |
Promise<ElementHandle<NodeFor<Selector>> | null>
An element matching the given selector.
Throws if an element matching the given selector doesn't appear.
import puppeteer from 'puppeteer';
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
let currentURL;
page
.mainFrame()
.waitForSelector('img')
.then(() => console.log('First URL with image: ' + currentURL));
for (currentURL of [
'https://example.com',
'https://google.com',
'https://bbc.com',
]) {
await page.goto(currentURL);
}
await browser.close();
})();