website/versioned_docs/version-24.40.0/api/puppeteer.frame.waitforselector.md
Waits for an element matching the given selector to appear in the frame.
This method works across navigations.
class Frame {
waitForSelector<Selector extends string>(
selector: Selector,
options?: WaitForSelectorOptions,
): Promise<ElementHandle<NodeFor<Selector>> | null>;
}
Parameter
</th><th>Type
</th><th>Description
</th></tr></thead> <tbody><tr><td>selector
</td><td>Selector
</td><td>The selector to query and wait for.
</td></tr> <tr><td>options
</td><td> </td><td>(Optional) Options for customizing waiting behavior.
</td></tr> </tbody></table>Returns:
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';
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();