2022-07-05 13:41:43 +00:00
|
|
|
---
|
|
|
|
sidebar_label: Frame.waitForSelector
|
|
|
|
---
|
|
|
|
|
|
|
|
# Frame.waitForSelector() method
|
|
|
|
|
2022-08-16 06:22:45 +00:00
|
|
|
Waits for an element matching the given selector to appear in the frame.
|
2022-08-10 21:34:29 +00:00
|
|
|
|
|
|
|
This method works across navigations.
|
|
|
|
|
2022-10-24 07:07:05 +00:00
|
|
|
#### Signature:
|
2022-07-05 13:41:43 +00:00
|
|
|
|
|
|
|
```typescript
|
|
|
|
class Frame {
|
2022-07-06 07:05:37 +00:00
|
|
|
waitForSelector<Selector extends string>(
|
2022-07-05 13:41:43 +00:00
|
|
|
selector: Selector,
|
|
|
|
options?: WaitForSelectorOptions
|
2022-07-06 07:05:37 +00:00
|
|
|
): Promise<ElementHandle<NodeFor<Selector>> | null>;
|
2022-07-05 13:41:43 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Parameters
|
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
| Parameter | Type | Description |
|
|
|
|
| --------- | --------------------------------------------------------------- | ----------------------------------------------------------- |
|
|
|
|
| selector | Selector | The selector to query and wait for. |
|
|
|
|
| options | [WaitForSelectorOptions](./puppeteer.waitforselectoroptions.md) | <i>(Optional)</i> Options for customizing waiting behavior. |
|
2022-07-05 13:41:43 +00:00
|
|
|
|
|
|
|
**Returns:**
|
|
|
|
|
2022-07-06 07:05:37 +00:00
|
|
|
Promise<[ElementHandle](./puppeteer.elementhandle.md)<[NodeFor](./puppeteer.nodefor.md)<Selector>> \| null>
|
2022-07-05 13:41:43 +00:00
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
An element matching the given selector.
|
2022-07-05 13:41:43 +00:00
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
## Exceptions
|
2022-07-05 13:41:43 +00:00
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
Throws if an element matching the given selector doesn't appear.
|
2022-07-05 13:41:43 +00:00
|
|
|
|
|
|
|
## Example
|
|
|
|
|
|
|
|
```ts
|
2022-12-09 12:57:39 +00:00
|
|
|
import puppeteer from 'puppeteer';
|
2022-07-05 13:41:43 +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();
|
|
|
|
})();
|
|
|
|
```
|