puppeteer/website/versioned_docs/version-22.10.0/api/puppeteer.elementhandle.waitforselector.md

98 lines
1.7 KiB
Markdown
Raw Normal View History

---
sidebar_label: ElementHandle.waitForSelector
---
# ElementHandle.waitForSelector() method
Wait for an element matching the given selector to appear in the current element.
Unlike [Frame.waitForSelector()](./puppeteer.frame.waitforselector.md), this method does not work across navigations or if the element is detached from DOM.
2022-10-24 14:31:12 +00:00
#### Signature:
```typescript
class ElementHandle {
waitForSelector<Selector extends string>(
selector: Selector,
options?: WaitForSelectorOptions
): Promise<ElementHandle<NodeFor<Selector>> | null>;
}
```
## Parameters
<table><thead><tr><th>
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>
[WaitForSelectorOptions](./puppeteer.waitforselectoroptions.md)
</td><td>
_(Optional)_ Options for customizing waiting behavior.
</td></tr>
</tbody></table>
**Returns:**
Promise&lt;[ElementHandle](./puppeteer.elementhandle.md)&lt;[NodeFor](./puppeteer.nodefor.md)&lt;Selector&gt;&gt; \| null&gt;
An element matching the given selector.
## Exceptions
Throws if an element matching the given selector doesn't appear.
## Example
```ts
2022-12-16 13:21:28 +00:00
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();
})();
```