2022-08-06 14:49:20 +00:00
---
2022-08-29 09:11:05 +00:00
sidebar_label: ElementHandle.waitForSelector
2022-08-06 14:49:20 +00:00
---
2022-08-29 09:11:05 +00:00
# ElementHandle.waitForSelector() method
2022-08-06 14:49:20 +00:00
2022-08-29 09:11:05 +00:00
Wait for an element matching the given selector to appear in the current element.
2022-08-16 11:58:16 +00:00
2022-08-29 09:11:05 +00:00
Unlike [Frame.waitForSelector() ](./puppeteer.frame.waitforselector.md ), this method does not work across navigations or if the element is detached from DOM.
2022-08-16 11:58:16 +00:00
2022-10-24 14:31:12 +00:00
#### Signature:
2022-08-06 14:49:20 +00:00
```typescript
2022-08-29 09:11:05 +00:00
class ElementHandle {
2022-08-06 14:49:20 +00:00
waitForSelector< Selector extends string > (
selector: Selector,
options?: WaitForSelectorOptions
): Promise< ElementHandle < NodeFor < Selector > > | null>;
}
```
## Parameters
2023-03-06 13:53:56 +00:00
| Parameter | Type | Description |
| --------- | --------------------------------------------------------------- | ------------------------------------------------------ |
| selector | Selector | The selector to query and wait for. |
| options | [WaitForSelectorOptions ](./puppeteer.waitforselectoroptions.md ) | _(Optional)_ Options for customizing waiting behavior. |
2022-08-06 14:49:20 +00:00
**Returns:**
Promise< [ElementHandle](./puppeteer.elementhandle.md)< [NodeFor](./puppeteer.nodefor.md)< Selector>> \| null>
2022-08-16 11:58:16 +00:00
An element matching the given selector.
2022-08-06 14:49:20 +00:00
2022-08-16 11:58:16 +00:00
## Exceptions
2022-08-06 14:49:20 +00:00
2022-08-16 11:58:16 +00:00
Throws if an element matching the given selector doesn't appear.
2022-08-06 14:49:20 +00:00
## Example
```ts
2022-12-16 13:21:28 +00:00
import puppeteer from 'puppeteer';
2022-08-06 14:49:20 +00:00
(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();
})();
```