Skip to main content
Version: 23.2.0

Frame.waitForSelector() 方法

¥Frame.waitForSelector() method

等待与给定选择器匹配的元素出现在框架中。

¥Waits for an element matching the given selector to appear in the frame.

此方法适用于各种导航。

¥This method works across navigations.

签名

¥Signature

class Frame {
waitForSelector<Selector extends string>(
selector: Selector,
options?: WaitForSelectorOptions
): Promise<ElementHandle<NodeFor<Selector>> | null>;
}

参数

¥Parameters

范围

类型

描述

selector

选择器

要查询和等待的选择器。

options

WaitForSelectorOptions

(可选的)用于自定义等待行为的选项。

Returns:

Promise<ElementHandle<NodeFor<Selector>> | null>

与给定选择器匹配的元素。

¥An element matching the given selector.

异常

¥Exceptions

如果未出现与给定选择器匹配的元素,则抛出该异常。

¥Throws if an element matching the given selector doesn't appear.

示例

¥Example

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();
})();