2023-05-15 14:39:47 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2023 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-09-15 11:00:20 +00:00
|
|
|
import type Protocol from 'devtools-protocol';
|
2023-09-13 13:47:55 +00:00
|
|
|
|
2023-09-26 16:24:24 +00:00
|
|
|
import type {ClickOptions, ElementHandle} from '../api/ElementHandle.js';
|
|
|
|
import type {HTTPResponse} from '../api/HTTPResponse.js';
|
|
|
|
import type {
|
|
|
|
Page,
|
|
|
|
WaitForSelectorOptions,
|
|
|
|
WaitTimeoutOptions,
|
2023-09-16 20:56:40 +00:00
|
|
|
} from '../api/Page.js';
|
2023-09-26 16:24:24 +00:00
|
|
|
import type {DeviceRequestPrompt} from '../cdp/DeviceRequestPrompt.js';
|
|
|
|
import type {IsolatedWorldChart} from '../cdp/IsolatedWorld.js';
|
|
|
|
import type {PuppeteerLifeCycleEvent} from '../cdp/LifecycleWatcher.js';
|
2023-09-15 11:00:20 +00:00
|
|
|
import {EventEmitter, type EventType} from '../common/EventEmitter.js';
|
2023-06-16 07:16:04 +00:00
|
|
|
import {getQueryHandlerAndSelector} from '../common/GetQueryHandler.js';
|
2023-08-23 14:58:18 +00:00
|
|
|
import {transposeIterableHandle} from '../common/HandleIterator.js';
|
2023-07-18 16:27:41 +00:00
|
|
|
import {LazyArg} from '../common/LazyArg.js';
|
2023-09-26 16:24:24 +00:00
|
|
|
import type {
|
|
|
|
Awaitable,
|
|
|
|
EvaluateFunc,
|
|
|
|
EvaluateFuncWith,
|
|
|
|
HandleFor,
|
|
|
|
NodeFor,
|
2023-05-15 14:39:47 +00:00
|
|
|
} from '../common/types.js';
|
2023-08-30 11:09:27 +00:00
|
|
|
import {
|
2023-09-05 07:10:16 +00:00
|
|
|
getPageContent,
|
2023-08-30 11:09:27 +00:00
|
|
|
importFSPromises,
|
|
|
|
withSourcePuppeteerURLIfNone,
|
|
|
|
} from '../common/util.js';
|
2023-09-05 07:10:16 +00:00
|
|
|
import {assert} from '../util/assert.js';
|
2023-08-31 14:39:32 +00:00
|
|
|
import {throwIfDisposed} from '../util/decorators.js';
|
2023-05-15 14:39:47 +00:00
|
|
|
|
2023-09-26 16:24:24 +00:00
|
|
|
import type {CDPSession} from './CDPSession.js';
|
|
|
|
import type {KeyboardTypeOptions} from './Input.js';
|
2023-09-15 11:00:20 +00:00
|
|
|
import {
|
|
|
|
FunctionLocator,
|
|
|
|
type Locator,
|
|
|
|
NodeLocator,
|
|
|
|
} from './locators/locators.js';
|
2023-09-26 16:24:24 +00:00
|
|
|
import type {Realm} from './Realm.js';
|
2023-06-16 07:16:04 +00:00
|
|
|
|
2023-09-18 12:14:06 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface WaitForOptions {
|
|
|
|
/**
|
|
|
|
* Maximum wait time in milliseconds. Pass 0 to disable the timeout.
|
|
|
|
*
|
|
|
|
* The default value can be changed by using the
|
|
|
|
* {@link Page.setDefaultTimeout} or {@link Page.setDefaultNavigationTimeout}
|
|
|
|
* methods.
|
|
|
|
*
|
|
|
|
* @defaultValue `30000`
|
|
|
|
*/
|
|
|
|
timeout?: number;
|
|
|
|
/**
|
|
|
|
* When to consider waiting succeeds. Given an array of event strings, waiting
|
|
|
|
* is considered to be successful after all events have been fired.
|
|
|
|
*
|
|
|
|
* @defaultValue `'load'`
|
|
|
|
*/
|
|
|
|
waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface GoToOptions extends WaitForOptions {
|
|
|
|
/**
|
|
|
|
* If provided, it will take preference over the referer header value set by
|
|
|
|
* {@link Page.setExtraHTTPHeaders | page.setExtraHTTPHeaders()}.
|
|
|
|
*/
|
|
|
|
referer?: string;
|
|
|
|
/**
|
|
|
|
* If provided, it will take preference over the referer-policy header value
|
|
|
|
* set by {@link Page.setExtraHTTPHeaders | page.setExtraHTTPHeaders()}.
|
|
|
|
*/
|
|
|
|
referrerPolicy?: string;
|
|
|
|
}
|
|
|
|
|
2023-05-15 14:39:47 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface FrameWaitForFunctionOptions {
|
|
|
|
/**
|
|
|
|
* An interval at which the `pageFunction` is executed, defaults to `raf`. If
|
|
|
|
* `polling` is a number, then it is treated as an interval in milliseconds at
|
|
|
|
* which the function would be executed. If `polling` is a string, then it can
|
|
|
|
* be one of the following values:
|
|
|
|
*
|
|
|
|
* - `raf` - to constantly execute `pageFunction` in `requestAnimationFrame`
|
|
|
|
* callback. This is the tightest polling mode which is suitable to observe
|
|
|
|
* styling changes.
|
|
|
|
*
|
|
|
|
* - `mutation` - to execute `pageFunction` on every DOM mutation.
|
|
|
|
*/
|
|
|
|
polling?: 'raf' | 'mutation' | number;
|
|
|
|
/**
|
|
|
|
* Maximum time to wait in milliseconds. Defaults to `30000` (30 seconds).
|
|
|
|
* Pass `0` to disable the timeout. Puppeteer's default timeout can be changed
|
|
|
|
* using {@link Page.setDefaultTimeout}.
|
|
|
|
*/
|
|
|
|
timeout?: number;
|
|
|
|
/**
|
|
|
|
* A signal object that allows you to cancel a waitForFunction call.
|
|
|
|
*/
|
|
|
|
signal?: AbortSignal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface FrameAddScriptTagOptions {
|
|
|
|
/**
|
|
|
|
* URL of the script to be added.
|
|
|
|
*/
|
|
|
|
url?: string;
|
|
|
|
/**
|
|
|
|
* Path to a JavaScript file to be injected into the frame.
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
* If `path` is a relative path, it is resolved relative to the current
|
|
|
|
* working directory (`process.cwd()` in Node.js).
|
|
|
|
*/
|
|
|
|
path?: string;
|
|
|
|
/**
|
|
|
|
* JavaScript to be injected into the frame.
|
|
|
|
*/
|
|
|
|
content?: string;
|
|
|
|
/**
|
|
|
|
* Sets the `type` of the script. Use `module` in order to load an ES2015 module.
|
|
|
|
*/
|
|
|
|
type?: string;
|
|
|
|
/**
|
|
|
|
* Sets the `id` of the script.
|
|
|
|
*/
|
|
|
|
id?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface FrameAddStyleTagOptions {
|
|
|
|
/**
|
|
|
|
* the URL of the CSS file to be added.
|
|
|
|
*/
|
|
|
|
url?: string;
|
|
|
|
/**
|
|
|
|
* The path to a CSS file to be injected into the frame.
|
|
|
|
* @remarks
|
|
|
|
* If `path` is a relative path, it is resolved relative to the current
|
|
|
|
* working directory (`process.cwd()` in Node.js).
|
|
|
|
*/
|
|
|
|
path?: string;
|
|
|
|
/**
|
|
|
|
* Raw CSS content to be injected into the frame.
|
|
|
|
*/
|
|
|
|
content?: string;
|
|
|
|
}
|
|
|
|
|
2023-09-13 13:47:55 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface FrameEvents extends Record<EventType, unknown> {
|
|
|
|
/** @internal */
|
|
|
|
[FrameEvent.FrameNavigated]: Protocol.Page.NavigationType;
|
|
|
|
/** @internal */
|
|
|
|
[FrameEvent.FrameSwapped]: undefined;
|
|
|
|
/** @internal */
|
|
|
|
[FrameEvent.LifecycleEvent]: undefined;
|
|
|
|
/** @internal */
|
|
|
|
[FrameEvent.FrameNavigatedWithinDocument]: undefined;
|
|
|
|
/** @internal */
|
|
|
|
[FrameEvent.FrameDetached]: Frame;
|
|
|
|
/** @internal */
|
|
|
|
[FrameEvent.FrameSwappedByActivation]: undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We use symbols to prevent external parties listening to these events.
|
|
|
|
* They are internal to Puppeteer.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
|
|
export namespace FrameEvent {
|
|
|
|
export const FrameNavigated = Symbol('Frame.FrameNavigated');
|
|
|
|
export const FrameSwapped = Symbol('Frame.FrameSwapped');
|
|
|
|
export const LifecycleEvent = Symbol('Frame.LifecycleEvent');
|
|
|
|
export const FrameNavigatedWithinDocument = Symbol(
|
|
|
|
'Frame.FrameNavigatedWithinDocument'
|
|
|
|
);
|
|
|
|
export const FrameDetached = Symbol('Frame.FrameDetached');
|
|
|
|
export const FrameSwappedByActivation = Symbol(
|
|
|
|
'Frame.FrameSwappedByActivation'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-31 14:39:32 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export const throwIfDetached = throwIfDisposed<Frame>(frame => {
|
|
|
|
return `Attempted to use detached Frame '${frame._id}'.`;
|
|
|
|
});
|
|
|
|
|
2023-05-15 14:39:47 +00:00
|
|
|
/**
|
|
|
|
* Represents a DOM frame.
|
|
|
|
*
|
|
|
|
* To understand frames, you can think of frames as `<iframe>` elements. Just
|
|
|
|
* like iframes, frames can be nested, and when JavaScript is executed in a
|
|
|
|
* frame, the JavaScript does not effect frames inside the ambient frame the
|
|
|
|
* JavaScript executes in.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* At any point in time, {@link Page | pages} expose their current frame
|
|
|
|
* tree via the {@link Page.mainFrame} and {@link Frame.childFrames} methods.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* An example of dumping frame tree:
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* import puppeteer from 'puppeteer';
|
|
|
|
*
|
|
|
|
* (async () => {
|
|
|
|
* const browser = await puppeteer.launch();
|
|
|
|
* const page = await browser.newPage();
|
|
|
|
* await page.goto('https://www.google.com/chrome/browser/canary.html');
|
|
|
|
* dumpFrameTree(page.mainFrame(), '');
|
|
|
|
* await browser.close();
|
|
|
|
*
|
|
|
|
* function dumpFrameTree(frame, indent) {
|
|
|
|
* console.log(indent + frame.url());
|
|
|
|
* for (const child of frame.childFrames()) {
|
|
|
|
* dumpFrameTree(child, indent + ' ');
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* })();
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* An example of getting text from an iframe element:
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const frame = page.frames().find(frame => frame.name() === 'myframe');
|
|
|
|
* const text = await frame.$eval('.selector', element => element.textContent);
|
|
|
|
* console.log(text);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
* Frame lifecycles are controlled by three events that are all dispatched on
|
|
|
|
* the parent {@link Frame.page | page}:
|
|
|
|
*
|
2023-09-13 13:47:55 +00:00
|
|
|
* - {@link PageEvent.FrameAttached}
|
|
|
|
* - {@link PageEvent.FrameNavigated}
|
|
|
|
* - {@link PageEvent.FrameDetached}
|
2023-05-15 14:39:47 +00:00
|
|
|
*
|
|
|
|
* @public
|
|
|
|
*/
|
2023-09-13 13:47:55 +00:00
|
|
|
export abstract class Frame extends EventEmitter<FrameEvents> {
|
2023-05-15 14:39:47 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_id!: string;
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_parentId?: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
worlds!: IsolatedWorldChart;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_name?: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_hasStartedLoading = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2023-08-08 14:42:45 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
2023-05-15 14:39:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The page associated with the frame.
|
|
|
|
*/
|
2023-08-30 11:09:27 +00:00
|
|
|
abstract page(): Page;
|
2023-05-15 14:39:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is `true` if the frame is an out-of-process (OOP) frame. Otherwise,
|
|
|
|
* `false`.
|
|
|
|
*/
|
2023-11-09 12:57:33 +00:00
|
|
|
abstract isOOPFrame(): boolean;
|
2023-05-15 14:39:47 +00:00
|
|
|
|
|
|
|
/**
|
2023-09-18 12:14:06 +00:00
|
|
|
* Navigates the frame to the given `url`.
|
2023-05-15 14:39:47 +00:00
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
* Navigation to `about:blank` or navigation to the same URL with a different
|
|
|
|
* hash will succeed and return `null`.
|
|
|
|
*
|
|
|
|
* :::warning
|
|
|
|
*
|
|
|
|
* Headless mode doesn't support navigation to a PDF document. See the {@link
|
|
|
|
* https://bugs.chromium.org/p/chromium/issues/detail?id=761295 | upstream
|
|
|
|
* issue}.
|
|
|
|
*
|
|
|
|
* :::
|
|
|
|
*
|
2023-09-18 12:14:06 +00:00
|
|
|
* @param url - URL to navigate the frame to. The URL should include scheme,
|
|
|
|
* e.g. `https://`
|
|
|
|
* @param options - Options to configure waiting behavior.
|
2023-05-15 14:39:47 +00:00
|
|
|
* @returns A promise which resolves to the main resource response. In case of
|
|
|
|
* multiple redirects, the navigation will resolve with the response of the
|
|
|
|
* last redirect.
|
2023-09-18 12:14:06 +00:00
|
|
|
* @throws If:
|
2023-05-15 14:39:47 +00:00
|
|
|
*
|
|
|
|
* - there's an SSL error (e.g. in case of self-signed certificates).
|
|
|
|
* - target URL is invalid.
|
2023-09-18 12:14:06 +00:00
|
|
|
* - the timeout is exceeded during navigation.
|
2023-05-15 14:39:47 +00:00
|
|
|
* - the remote server does not respond or is unreachable.
|
|
|
|
* - the main resource failed to load.
|
|
|
|
*
|
|
|
|
* This method will not throw an error when any valid HTTP status code is
|
|
|
|
* returned by the remote server, including 404 "Not Found" and 500 "Internal
|
|
|
|
* Server Error". The status code for such responses can be retrieved by
|
|
|
|
* calling {@link HTTPResponse.status}.
|
|
|
|
*/
|
2023-08-30 11:09:27 +00:00
|
|
|
abstract goto(
|
2023-05-15 14:39:47 +00:00
|
|
|
url: string,
|
|
|
|
options?: {
|
|
|
|
referer?: string;
|
|
|
|
referrerPolicy?: string;
|
|
|
|
timeout?: number;
|
|
|
|
waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[];
|
|
|
|
}
|
|
|
|
): Promise<HTTPResponse | null>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Waits for the frame to navigate. It is useful for when you run code which
|
|
|
|
* will indirectly cause the frame to navigate.
|
|
|
|
*
|
|
|
|
* Usage of the
|
|
|
|
* {@link https://developer.mozilla.org/en-US/docs/Web/API/History_API | History API}
|
|
|
|
* to change the URL is considered a navigation.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const [response] = await Promise.all([
|
|
|
|
* // The navigation promise resolves after navigation has finished
|
|
|
|
* frame.waitForNavigation(),
|
|
|
|
* // Clicking the link will indirectly cause a navigation
|
|
|
|
* frame.click('a.my-link'),
|
|
|
|
* ]);
|
|
|
|
* ```
|
|
|
|
*
|
2023-09-18 12:14:06 +00:00
|
|
|
* @param options - Options to configure waiting behavior.
|
|
|
|
* @returns A promise which resolves to the main resource response.
|
2023-05-15 14:39:47 +00:00
|
|
|
*/
|
2023-09-18 12:14:06 +00:00
|
|
|
abstract waitForNavigation(
|
|
|
|
options?: WaitForOptions
|
|
|
|
): Promise<HTTPResponse | null>;
|
2023-05-15 14:39:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2023-09-01 12:12:29 +00:00
|
|
|
abstract get client(): CDPSession;
|
2023-05-15 14:39:47 +00:00
|
|
|
|
2023-06-16 07:16:04 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2023-08-30 11:09:27 +00:00
|
|
|
abstract mainRealm(): Realm;
|
2023-06-16 07:16:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2023-08-30 11:09:27 +00:00
|
|
|
abstract isolatedRealm(): Realm;
|
2023-06-16 07:16:04 +00:00
|
|
|
|
2023-09-11 12:07:52 +00:00
|
|
|
#_document: Promise<ElementHandle<Document>> | undefined;
|
|
|
|
|
2023-09-05 07:10:16 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2023-09-11 12:07:52 +00:00
|
|
|
#document(): Promise<ElementHandle<Document>> {
|
|
|
|
if (!this.#_document) {
|
|
|
|
this.#_document = this.isolatedRealm()
|
|
|
|
.evaluateHandle(() => {
|
|
|
|
return document;
|
|
|
|
})
|
|
|
|
.then(handle => {
|
|
|
|
return this.mainRealm().transferHandle(handle);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this.#_document;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to clear the document handle that has been destroyed.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
clearDocumentHandle(): void {
|
|
|
|
this.#_document = undefined;
|
2023-09-05 07:10:16 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:58:18 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
@throwIfDetached
|
2023-08-23 14:58:18 +00:00
|
|
|
async frameElement(): Promise<HandleFor<HTMLIFrameElement> | null> {
|
|
|
|
const parentFrame = this.parentFrame();
|
|
|
|
if (!parentFrame) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-08-30 10:02:59 +00:00
|
|
|
using list = await parentFrame.isolatedRealm().evaluateHandle(() => {
|
2023-08-23 14:58:18 +00:00
|
|
|
return document.querySelectorAll('iframe');
|
|
|
|
});
|
2023-08-30 10:02:59 +00:00
|
|
|
for await (using iframe of transposeIterableHandle(list)) {
|
2023-08-23 14:58:18 +00:00
|
|
|
const frame = await iframe.contentFrame();
|
|
|
|
if (frame._id === this._id) {
|
2023-08-30 10:02:59 +00:00
|
|
|
return iframe.move();
|
2023-08-23 14:58:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-05-15 14:39:47 +00:00
|
|
|
/**
|
|
|
|
* Behaves identically to {@link Page.evaluateHandle} except it's run within
|
|
|
|
* the context of this frame.
|
|
|
|
*
|
|
|
|
* @see {@link Page.evaluateHandle} for details.
|
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
@throwIfDetached
|
2023-05-15 14:39:47 +00:00
|
|
|
async evaluateHandle<
|
|
|
|
Params extends unknown[],
|
2023-07-17 08:52:54 +00:00
|
|
|
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>,
|
2023-05-15 14:39:47 +00:00
|
|
|
>(
|
|
|
|
pageFunction: Func | string,
|
|
|
|
...args: Params
|
2023-08-30 11:09:27 +00:00
|
|
|
): Promise<HandleFor<Awaited<ReturnType<Func>>>> {
|
|
|
|
pageFunction = withSourcePuppeteerURLIfNone(
|
|
|
|
this.evaluateHandle.name,
|
|
|
|
pageFunction
|
|
|
|
);
|
2023-09-01 07:49:33 +00:00
|
|
|
return await this.mainRealm().evaluateHandle(pageFunction, ...args);
|
2023-05-15 14:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Behaves identically to {@link Page.evaluate} except it's run within the
|
|
|
|
* the context of this frame.
|
|
|
|
*
|
|
|
|
* @see {@link Page.evaluate} for details.
|
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
@throwIfDetached
|
2023-05-15 14:39:47 +00:00
|
|
|
async evaluate<
|
|
|
|
Params extends unknown[],
|
2023-07-17 08:52:54 +00:00
|
|
|
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>,
|
2023-05-15 14:39:47 +00:00
|
|
|
>(
|
|
|
|
pageFunction: Func | string,
|
|
|
|
...args: Params
|
2023-08-30 11:09:27 +00:00
|
|
|
): Promise<Awaited<ReturnType<Func>>> {
|
|
|
|
pageFunction = withSourcePuppeteerURLIfNone(
|
|
|
|
this.evaluate.name,
|
|
|
|
pageFunction
|
|
|
|
);
|
2023-09-01 07:49:33 +00:00
|
|
|
return await this.mainRealm().evaluate(pageFunction, ...args);
|
2023-05-15 14:39:47 +00:00
|
|
|
}
|
|
|
|
|
2023-06-02 17:46:10 +00:00
|
|
|
/**
|
2023-07-27 07:23:28 +00:00
|
|
|
* Creates a locator for the provided selector. See {@link Locator} for
|
2023-06-02 17:46:10 +00:00
|
|
|
* details and supported actions.
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
* Locators API is experimental and we will not follow semver for breaking
|
|
|
|
* change in the Locators API.
|
|
|
|
*/
|
2023-07-19 15:39:38 +00:00
|
|
|
locator<Selector extends string>(
|
|
|
|
selector: Selector
|
2023-07-27 07:23:28 +00:00
|
|
|
): Locator<NodeFor<Selector>>;
|
2023-06-02 17:46:10 +00:00
|
|
|
|
2023-07-27 07:23:28 +00:00
|
|
|
/**
|
|
|
|
* Creates a locator for the provided function. See {@link Locator} for
|
|
|
|
* details and supported actions.
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
* Locators API is experimental and we will not follow semver for breaking
|
|
|
|
* change in the Locators API.
|
|
|
|
*/
|
|
|
|
locator<Ret>(func: () => Awaitable<Ret>): Locator<Ret>;
|
2023-08-31 14:39:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
@throwIfDetached
|
2023-07-27 07:23:28 +00:00
|
|
|
locator<Selector extends string, Ret>(
|
|
|
|
selectorOrFunc: Selector | (() => Awaitable<Ret>)
|
|
|
|
): Locator<NodeFor<Selector>> | Locator<Ret> {
|
|
|
|
if (typeof selectorOrFunc === 'string') {
|
|
|
|
return NodeLocator.create(this, selectorOrFunc);
|
|
|
|
} else {
|
|
|
|
return FunctionLocator.create(this, selectorOrFunc);
|
|
|
|
}
|
|
|
|
}
|
2023-05-15 14:39:47 +00:00
|
|
|
/**
|
|
|
|
* Queries the frame for an element matching the given selector.
|
|
|
|
*
|
|
|
|
* @param selector - The selector to query for.
|
|
|
|
* @returns A {@link ElementHandle | element handle} to the first element
|
|
|
|
* matching the given selector. Otherwise, `null`.
|
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
@throwIfDetached
|
2023-05-15 14:39:47 +00:00
|
|
|
async $<Selector extends string>(
|
|
|
|
selector: Selector
|
2023-08-30 11:09:27 +00:00
|
|
|
): Promise<ElementHandle<NodeFor<Selector>> | null> {
|
2023-09-11 12:07:52 +00:00
|
|
|
// eslint-disable-next-line rulesdir/use-using -- This is cached.
|
|
|
|
const document = await this.#document();
|
2023-09-05 07:10:16 +00:00
|
|
|
return await document.$(selector);
|
2023-05-15 14:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queries the frame for all elements matching the given selector.
|
|
|
|
*
|
|
|
|
* @param selector - The selector to query for.
|
|
|
|
* @returns An array of {@link ElementHandle | element handles} that point to
|
|
|
|
* elements matching the given selector.
|
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
@throwIfDetached
|
2023-05-15 14:39:47 +00:00
|
|
|
async $$<Selector extends string>(
|
|
|
|
selector: Selector
|
2023-08-30 11:09:27 +00:00
|
|
|
): Promise<Array<ElementHandle<NodeFor<Selector>>>> {
|
2023-09-11 12:07:52 +00:00
|
|
|
// eslint-disable-next-line rulesdir/use-using -- This is cached.
|
|
|
|
const document = await this.#document();
|
2023-09-05 07:10:16 +00:00
|
|
|
return await document.$$(selector);
|
2023-05-15 14:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs the given function on the first element matching the given selector in
|
|
|
|
* the frame.
|
|
|
|
*
|
|
|
|
* If the given function returns a promise, then this method will wait till
|
|
|
|
* the promise resolves.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const searchValue = await frame.$eval('#search', el => el.value);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param selector - The selector to query for.
|
|
|
|
* @param pageFunction - The function to be evaluated in the frame's context.
|
|
|
|
* The first element matching the selector will be passed to the function as
|
|
|
|
* its first argument.
|
|
|
|
* @param args - Additional arguments to pass to `pageFunction`.
|
|
|
|
* @returns A promise to the result of the function.
|
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
@throwIfDetached
|
2023-09-01 12:12:29 +00:00
|
|
|
async $eval<
|
2023-05-15 14:39:47 +00:00
|
|
|
Selector extends string,
|
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFuncWith<NodeFor<Selector>, Params> = EvaluateFuncWith<
|
|
|
|
NodeFor<Selector>,
|
|
|
|
Params
|
2023-07-17 08:52:54 +00:00
|
|
|
>,
|
2023-05-15 14:39:47 +00:00
|
|
|
>(
|
|
|
|
selector: Selector,
|
2023-08-30 11:09:27 +00:00
|
|
|
pageFunction: string | Func,
|
2023-05-15 14:39:47 +00:00
|
|
|
...args: Params
|
2023-08-30 11:09:27 +00:00
|
|
|
): Promise<Awaited<ReturnType<Func>>> {
|
|
|
|
pageFunction = withSourcePuppeteerURLIfNone(this.$eval.name, pageFunction);
|
2023-09-11 12:07:52 +00:00
|
|
|
// eslint-disable-next-line rulesdir/use-using -- This is cached.
|
|
|
|
const document = await this.#document();
|
2023-09-05 07:10:16 +00:00
|
|
|
return await document.$eval(selector, pageFunction, ...args);
|
2023-05-15 14:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs the given function on an array of elements matching the given selector
|
|
|
|
* in the frame.
|
|
|
|
*
|
|
|
|
* If the given function returns a promise, then this method will wait till
|
|
|
|
* the promise resolves.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
2023-11-14 12:39:58 +00:00
|
|
|
* ```ts
|
2023-05-15 14:39:47 +00:00
|
|
|
* const divsCounts = await frame.$$eval('div', divs => divs.length);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param selector - The selector to query for.
|
|
|
|
* @param pageFunction - The function to be evaluated in the frame's context.
|
|
|
|
* An array of elements matching the given selector will be passed to the
|
|
|
|
* function as its first argument.
|
|
|
|
* @param args - Additional arguments to pass to `pageFunction`.
|
|
|
|
* @returns A promise to the result of the function.
|
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
@throwIfDetached
|
2023-09-01 12:12:29 +00:00
|
|
|
async $$eval<
|
2023-05-15 14:39:47 +00:00
|
|
|
Selector extends string,
|
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFuncWith<
|
|
|
|
Array<NodeFor<Selector>>,
|
|
|
|
Params
|
2023-07-17 08:52:54 +00:00
|
|
|
> = EvaluateFuncWith<Array<NodeFor<Selector>>, Params>,
|
2023-05-15 14:39:47 +00:00
|
|
|
>(
|
|
|
|
selector: Selector,
|
2023-08-30 11:09:27 +00:00
|
|
|
pageFunction: string | Func,
|
2023-05-15 14:39:47 +00:00
|
|
|
...args: Params
|
2023-08-30 11:09:27 +00:00
|
|
|
): Promise<Awaited<ReturnType<Func>>> {
|
|
|
|
pageFunction = withSourcePuppeteerURLIfNone(this.$$eval.name, pageFunction);
|
2023-09-11 12:07:52 +00:00
|
|
|
// eslint-disable-next-line rulesdir/use-using -- This is cached.
|
|
|
|
const document = await this.#document();
|
2023-09-05 07:10:16 +00:00
|
|
|
return await document.$$eval(selector, pageFunction, ...args);
|
2023-05-15 14:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated Use {@link Frame.$$} with the `xpath` prefix.
|
|
|
|
*
|
|
|
|
* Example: `await frame.$$('xpath/' + xpathExpression)`
|
|
|
|
*
|
|
|
|
* This method evaluates the given XPath expression and returns the results.
|
|
|
|
* If `xpath` starts with `//` instead of `.//`, the dot will be appended
|
|
|
|
* automatically.
|
|
|
|
* @param expression - the XPath expression to evaluate.
|
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
@throwIfDetached
|
2023-09-01 12:12:29 +00:00
|
|
|
async $x(expression: string): Promise<Array<ElementHandle<Node>>> {
|
2023-09-11 12:07:52 +00:00
|
|
|
// eslint-disable-next-line rulesdir/use-using -- This is cached.
|
|
|
|
const document = await this.#document();
|
2023-09-05 07:10:16 +00:00
|
|
|
return await document.$x(expression);
|
2023-05-15 14:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Waits for an element matching the given selector to appear in the frame.
|
|
|
|
*
|
|
|
|
* This method works across navigations.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* 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();
|
|
|
|
* })();
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param selector - The selector to query and wait for.
|
|
|
|
* @param options - Options for customizing waiting behavior.
|
|
|
|
* @returns An element matching the given selector.
|
|
|
|
* @throws Throws if an element matching the given selector doesn't appear.
|
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
@throwIfDetached
|
2023-05-15 14:39:47 +00:00
|
|
|
async waitForSelector<Selector extends string>(
|
|
|
|
selector: Selector,
|
2023-06-16 07:16:04 +00:00
|
|
|
options: WaitForSelectorOptions = {}
|
|
|
|
): Promise<ElementHandle<NodeFor<Selector>> | null> {
|
|
|
|
const {updatedSelector, QueryHandler} =
|
|
|
|
getQueryHandlerAndSelector(selector);
|
|
|
|
return (await QueryHandler.waitFor(
|
|
|
|
this,
|
|
|
|
updatedSelector,
|
|
|
|
options
|
|
|
|
)) as ElementHandle<NodeFor<Selector>> | null;
|
2023-05-15 14:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated Use {@link Frame.waitForSelector} with the `xpath` prefix.
|
|
|
|
*
|
|
|
|
* Example: `await frame.waitForSelector('xpath/' + xpathExpression)`
|
|
|
|
*
|
|
|
|
* The method evaluates the XPath expression relative to the Frame.
|
|
|
|
* If `xpath` starts with `//` instead of `.//`, the dot will be appended
|
|
|
|
* automatically.
|
|
|
|
*
|
|
|
|
* Wait for the `xpath` to appear in page. If at the moment of calling the
|
|
|
|
* method the `xpath` already exists, the method will return immediately. If
|
|
|
|
* the xpath doesn't appear after the `timeout` milliseconds of waiting, the
|
|
|
|
* function will throw.
|
|
|
|
*
|
|
|
|
* For a code example, see the example for {@link Frame.waitForSelector}. That
|
|
|
|
* function behaves identically other than taking a CSS selector rather than
|
|
|
|
* an XPath.
|
|
|
|
*
|
|
|
|
* @param xpath - the XPath expression to wait for.
|
|
|
|
* @param options - options to configure the visibility of the element and how
|
|
|
|
* long to wait before timing out.
|
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
@throwIfDetached
|
2023-05-15 14:39:47 +00:00
|
|
|
async waitForXPath(
|
|
|
|
xpath: string,
|
2023-06-16 07:16:04 +00:00
|
|
|
options: WaitForSelectorOptions = {}
|
|
|
|
): Promise<ElementHandle<Node> | null> {
|
|
|
|
if (xpath.startsWith('//')) {
|
|
|
|
xpath = `.${xpath}`;
|
|
|
|
}
|
2023-09-01 07:49:33 +00:00
|
|
|
return await this.waitForSelector(`xpath/${xpath}`, options);
|
2023-05-15 14:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @example
|
|
|
|
* The `waitForFunction` can be used to observe viewport size change:
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* import puppeteer from 'puppeteer';
|
|
|
|
*
|
|
|
|
* (async () => {
|
|
|
|
* . const browser = await puppeteer.launch();
|
|
|
|
* . const page = await browser.newPage();
|
|
|
|
* . const watchDog = page.mainFrame().waitForFunction('window.innerWidth < 100');
|
|
|
|
* . page.setViewport({width: 50, height: 50});
|
|
|
|
* . await watchDog;
|
|
|
|
* . await browser.close();
|
|
|
|
* })();
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* To pass arguments from Node.js to the predicate of `page.waitForFunction` function:
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const selector = '.foo';
|
|
|
|
* await frame.waitForFunction(
|
|
|
|
* selector => !!document.querySelector(selector),
|
|
|
|
* {}, // empty options object
|
|
|
|
* selector
|
|
|
|
* );
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param pageFunction - the function to evaluate in the frame context.
|
|
|
|
* @param options - options to configure the polling method and timeout.
|
|
|
|
* @param args - arguments to pass to the `pageFunction`.
|
|
|
|
* @returns the promise which resolve when the `pageFunction` returns a truthy value.
|
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
@throwIfDetached
|
2023-09-01 12:12:29 +00:00
|
|
|
async waitForFunction<
|
2023-05-15 14:39:47 +00:00
|
|
|
Params extends unknown[],
|
2023-07-17 08:52:54 +00:00
|
|
|
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>,
|
2023-05-15 14:39:47 +00:00
|
|
|
>(
|
|
|
|
pageFunction: Func | string,
|
2023-06-16 07:16:04 +00:00
|
|
|
options: FrameWaitForFunctionOptions = {},
|
2023-05-15 14:39:47 +00:00
|
|
|
...args: Params
|
2023-06-16 07:16:04 +00:00
|
|
|
): Promise<HandleFor<Awaited<ReturnType<Func>>>> {
|
2023-09-01 12:12:29 +00:00
|
|
|
return await (this.mainRealm().waitForFunction(
|
2023-06-16 07:16:04 +00:00
|
|
|
pageFunction,
|
|
|
|
options,
|
|
|
|
...args
|
2023-09-01 12:12:29 +00:00
|
|
|
) as Promise<HandleFor<Awaited<ReturnType<Func>>>>);
|
2023-05-15 14:39:47 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* The full HTML contents of the frame, including the DOCTYPE.
|
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
@throwIfDetached
|
2023-09-01 12:12:29 +00:00
|
|
|
async content(): Promise<string> {
|
2023-09-05 07:10:16 +00:00
|
|
|
return await this.evaluate(getPageContent);
|
2023-05-15 14:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the content of the frame.
|
|
|
|
*
|
|
|
|
* @param html - HTML markup to assign to the page.
|
|
|
|
* @param options - Options to configure how long before timing out and at
|
|
|
|
* what point to consider the content setting successful.
|
|
|
|
*/
|
2023-08-30 11:09:27 +00:00
|
|
|
abstract setContent(
|
2023-05-15 14:39:47 +00:00
|
|
|
html: string,
|
|
|
|
options?: {
|
|
|
|
timeout?: number;
|
|
|
|
waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[];
|
|
|
|
}
|
|
|
|
): Promise<void>;
|
|
|
|
|
2023-11-29 17:25:54 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
async setFrameContent(content: string): Promise<void> {
|
|
|
|
return await this.evaluate(html => {
|
|
|
|
document.open();
|
|
|
|
document.write(html);
|
|
|
|
document.close();
|
|
|
|
}, content);
|
|
|
|
}
|
|
|
|
|
2023-05-15 14:39:47 +00:00
|
|
|
/**
|
|
|
|
* The frame's `name` attribute as specified in the tag.
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
* If the name is empty, it returns the `id` attribute instead.
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
* This value is calculated once when the frame is created, and will not
|
|
|
|
* update if the attribute is changed later.
|
|
|
|
*/
|
|
|
|
name(): string {
|
|
|
|
return this._name || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The frame's URL.
|
|
|
|
*/
|
2023-08-30 11:09:27 +00:00
|
|
|
abstract url(): string;
|
2023-05-15 14:39:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The parent frame, if any. Detached and main frames return `null`.
|
|
|
|
*/
|
2023-08-30 11:09:27 +00:00
|
|
|
abstract parentFrame(): Frame | null;
|
2023-05-15 14:39:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An array of child frames.
|
|
|
|
*/
|
2023-08-30 11:09:27 +00:00
|
|
|
abstract childFrames(): Frame[];
|
2023-05-15 14:39:47 +00:00
|
|
|
|
2023-08-31 14:39:32 +00:00
|
|
|
/**
|
|
|
|
* @returns `true` if the frame has detached. `false` otherwise.
|
|
|
|
*/
|
|
|
|
abstract get detached(): boolean;
|
|
|
|
|
2023-05-15 14:39:47 +00:00
|
|
|
/**
|
|
|
|
* Is`true` if the frame has been detached. Otherwise, `false`.
|
2023-08-31 14:39:32 +00:00
|
|
|
*
|
|
|
|
* @deprecated Use the `detached` getter.
|
2023-05-15 14:39:47 +00:00
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
isDetached(): boolean {
|
|
|
|
return this.detached;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
get disposed(): boolean {
|
|
|
|
return this.detached;
|
|
|
|
}
|
2023-05-15 14:39:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a `<script>` tag into the page with the desired url or content.
|
|
|
|
*
|
|
|
|
* @param options - Options for the script.
|
|
|
|
* @returns An {@link ElementHandle | element handle} to the injected
|
|
|
|
* `<script>` element.
|
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
@throwIfDetached
|
2023-05-15 14:39:47 +00:00
|
|
|
async addScriptTag(
|
|
|
|
options: FrameAddScriptTagOptions
|
2023-07-18 19:55:30 +00:00
|
|
|
): Promise<ElementHandle<HTMLScriptElement>> {
|
|
|
|
let {content = '', type} = options;
|
|
|
|
const {path} = options;
|
|
|
|
if (+!!options.url + +!!path + +!!content !== 1) {
|
|
|
|
throw new Error(
|
|
|
|
'Exactly one of `url`, `path`, or `content` must be specified.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path) {
|
|
|
|
const fs = await importFSPromises();
|
|
|
|
content = await fs.readFile(path, 'utf8');
|
|
|
|
content += `//# sourceURL=${path.replace(/\n/g, '')}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
type = type ?? 'text/javascript';
|
|
|
|
|
2023-09-01 07:49:33 +00:00
|
|
|
return await this.mainRealm().transferHandle(
|
2023-07-18 19:55:30 +00:00
|
|
|
await this.isolatedRealm().evaluateHandle(
|
|
|
|
async ({Deferred}, {url, id, type, content}) => {
|
|
|
|
const deferred = Deferred.create<void>();
|
|
|
|
const script = document.createElement('script');
|
|
|
|
script.type = type;
|
|
|
|
script.text = content;
|
|
|
|
if (url) {
|
|
|
|
script.src = url;
|
|
|
|
script.addEventListener(
|
|
|
|
'load',
|
|
|
|
() => {
|
|
|
|
return deferred.resolve();
|
|
|
|
},
|
|
|
|
{once: true}
|
|
|
|
);
|
|
|
|
script.addEventListener(
|
|
|
|
'error',
|
|
|
|
event => {
|
|
|
|
deferred.reject(
|
|
|
|
new Error(event.message ?? 'Could not load script')
|
|
|
|
);
|
|
|
|
},
|
|
|
|
{once: true}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
if (id) {
|
|
|
|
script.id = id;
|
|
|
|
}
|
|
|
|
document.head.appendChild(script);
|
|
|
|
await deferred.valueOrThrow();
|
|
|
|
return script;
|
|
|
|
},
|
|
|
|
LazyArg.create(context => {
|
|
|
|
return context.puppeteerUtil;
|
|
|
|
}),
|
|
|
|
{...options, type, content}
|
|
|
|
)
|
|
|
|
);
|
2023-05-15 14:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-08-31 14:39:32 +00:00
|
|
|
* Adds a `HTMLStyleElement` into the frame with the desired URL
|
2023-05-15 14:39:47 +00:00
|
|
|
*
|
2023-08-31 14:39:32 +00:00
|
|
|
* @returns An {@link ElementHandle | element handle} to the loaded `<style>`
|
|
|
|
* element.
|
2023-05-15 14:39:47 +00:00
|
|
|
*/
|
|
|
|
async addStyleTag(
|
|
|
|
options: Omit<FrameAddStyleTagOptions, 'url'>
|
|
|
|
): Promise<ElementHandle<HTMLStyleElement>>;
|
2023-08-31 14:39:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a `HTMLLinkElement` into the frame with the desired URL
|
|
|
|
*
|
|
|
|
* @returns An {@link ElementHandle | element handle} to the loaded `<link>`
|
|
|
|
* element.
|
|
|
|
*/
|
2023-05-15 14:39:47 +00:00
|
|
|
async addStyleTag(
|
|
|
|
options: FrameAddStyleTagOptions
|
|
|
|
): Promise<ElementHandle<HTMLLinkElement>>;
|
2023-08-31 14:39:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
@throwIfDetached
|
2023-07-18 16:27:41 +00:00
|
|
|
async addStyleTag(
|
|
|
|
options: FrameAddStyleTagOptions
|
|
|
|
): Promise<ElementHandle<HTMLStyleElement | HTMLLinkElement>> {
|
|
|
|
let {content = ''} = options;
|
|
|
|
const {path} = options;
|
|
|
|
if (+!!options.url + +!!path + +!!content !== 1) {
|
|
|
|
throw new Error(
|
|
|
|
'Exactly one of `url`, `path`, or `content` must be specified.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path) {
|
|
|
|
const fs = await importFSPromises();
|
|
|
|
|
|
|
|
content = await fs.readFile(path, 'utf8');
|
|
|
|
content += '/*# sourceURL=' + path.replace(/\n/g, '') + '*/';
|
|
|
|
options.content = content;
|
|
|
|
}
|
|
|
|
|
2023-09-01 07:49:33 +00:00
|
|
|
return await this.mainRealm().transferHandle(
|
2023-07-18 16:27:41 +00:00
|
|
|
await this.isolatedRealm().evaluateHandle(
|
|
|
|
async ({Deferred}, {url, content}) => {
|
|
|
|
const deferred = Deferred.create<void>();
|
|
|
|
let element: HTMLStyleElement | HTMLLinkElement;
|
|
|
|
if (!url) {
|
|
|
|
element = document.createElement('style');
|
|
|
|
element.appendChild(document.createTextNode(content!));
|
|
|
|
} else {
|
|
|
|
const link = document.createElement('link');
|
|
|
|
link.rel = 'stylesheet';
|
|
|
|
link.href = url;
|
|
|
|
element = link;
|
|
|
|
}
|
|
|
|
element.addEventListener(
|
|
|
|
'load',
|
|
|
|
() => {
|
|
|
|
deferred.resolve();
|
|
|
|
},
|
|
|
|
{once: true}
|
|
|
|
);
|
|
|
|
element.addEventListener(
|
|
|
|
'error',
|
|
|
|
event => {
|
|
|
|
deferred.reject(
|
|
|
|
new Error(
|
|
|
|
(event as ErrorEvent).message ?? 'Could not load style'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
{once: true}
|
|
|
|
);
|
|
|
|
document.head.appendChild(element);
|
|
|
|
await deferred.valueOrThrow();
|
|
|
|
return element;
|
|
|
|
},
|
|
|
|
LazyArg.create(context => {
|
|
|
|
return context.puppeteerUtil;
|
|
|
|
}),
|
|
|
|
options
|
|
|
|
)
|
|
|
|
);
|
2023-05-15 14:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clicks the first element found that matches `selector`.
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
* If `click()` triggers a navigation event and there's a separate
|
|
|
|
* `page.waitForNavigation()` promise to be resolved, you may end up with a
|
|
|
|
* race condition that yields unexpected results. The correct pattern for
|
|
|
|
* click and wait for navigation is the following:
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const [response] = await Promise.all([
|
|
|
|
* page.waitForNavigation(waitOptions),
|
|
|
|
* frame.click(selector, clickOptions),
|
|
|
|
* ]);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param selector - The selector to query for.
|
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
@throwIfDetached
|
2023-09-01 12:12:29 +00:00
|
|
|
async click(
|
|
|
|
selector: string,
|
|
|
|
options: Readonly<ClickOptions> = {}
|
|
|
|
): Promise<void> {
|
2023-09-05 07:10:16 +00:00
|
|
|
using handle = await this.$(selector);
|
|
|
|
assert(handle, `No element found for selector: ${selector}`);
|
|
|
|
await handle.click(options);
|
|
|
|
await handle.dispose();
|
2023-05-15 14:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Focuses the first element that matches the `selector`.
|
|
|
|
*
|
|
|
|
* @param selector - The selector to query for.
|
|
|
|
* @throws Throws if there's no element matching `selector`.
|
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
@throwIfDetached
|
2023-09-01 12:12:29 +00:00
|
|
|
async focus(selector: string): Promise<void> {
|
2023-09-05 07:10:16 +00:00
|
|
|
using handle = await this.$(selector);
|
|
|
|
assert(handle, `No element found for selector: ${selector}`);
|
|
|
|
await handle.focus();
|
2023-05-15 14:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hovers the pointer over the center of the first element that matches the
|
|
|
|
* `selector`.
|
|
|
|
*
|
|
|
|
* @param selector - The selector to query for.
|
|
|
|
* @throws Throws if there's no element matching `selector`.
|
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
@throwIfDetached
|
2023-09-01 12:12:29 +00:00
|
|
|
async hover(selector: string): Promise<void> {
|
2023-09-05 07:10:16 +00:00
|
|
|
using handle = await this.$(selector);
|
|
|
|
assert(handle, `No element found for selector: ${selector}`);
|
|
|
|
await handle.hover();
|
2023-05-15 14:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Selects a set of value on the first `<select>` element that matches the
|
|
|
|
* `selector`.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* frame.select('select#colors', 'blue'); // single selection
|
|
|
|
* frame.select('select#colors', 'red', 'green', 'blue'); // multiple selections
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param selector - The selector to query for.
|
|
|
|
* @param values - The array of values to select. If the `<select>` has the
|
|
|
|
* `multiple` attribute, all values are considered, otherwise only the first
|
|
|
|
* one is taken into account.
|
|
|
|
* @returns the list of values that were successfully selected.
|
|
|
|
* @throws Throws if there's no `<select>` matching `selector`.
|
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
@throwIfDetached
|
2023-09-01 12:12:29 +00:00
|
|
|
async select(selector: string, ...values: string[]): Promise<string[]> {
|
2023-09-05 07:10:16 +00:00
|
|
|
using handle = await this.$(selector);
|
|
|
|
assert(handle, `No element found for selector: ${selector}`);
|
|
|
|
return await handle.select(...values);
|
2023-05-15 14:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Taps the first element that matches the `selector`.
|
|
|
|
*
|
|
|
|
* @param selector - The selector to query for.
|
|
|
|
* @throws Throws if there's no element matching `selector`.
|
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
@throwIfDetached
|
2023-09-01 12:12:29 +00:00
|
|
|
async tap(selector: string): Promise<void> {
|
2023-09-05 07:10:16 +00:00
|
|
|
using handle = await this.$(selector);
|
|
|
|
assert(handle, `No element found for selector: ${selector}`);
|
|
|
|
await handle.tap();
|
2023-05-15 14:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a `keydown`, `keypress`/`input`, and `keyup` event for each character
|
|
|
|
* in the text.
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
* To press a special key, like `Control` or `ArrowDown`, use
|
|
|
|
* {@link Keyboard.press}.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* await frame.type('#mytextarea', 'Hello'); // Types instantly
|
|
|
|
* await frame.type('#mytextarea', 'World', {delay: 100}); // Types slower, like a user
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param selector - the selector for the element to type into. If there are
|
|
|
|
* multiple the first will be used.
|
|
|
|
* @param text - text to type into the element
|
|
|
|
* @param options - takes one option, `delay`, which sets the time to wait
|
|
|
|
* between key presses in milliseconds. Defaults to `0`.
|
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
@throwIfDetached
|
2023-09-01 12:12:29 +00:00
|
|
|
async type(
|
2023-05-15 14:39:47 +00:00
|
|
|
selector: string,
|
|
|
|
text: string,
|
2023-06-19 15:44:39 +00:00
|
|
|
options?: Readonly<KeyboardTypeOptions>
|
2023-06-19 11:26:30 +00:00
|
|
|
): Promise<void> {
|
2023-09-05 07:10:16 +00:00
|
|
|
using handle = await this.$(selector);
|
|
|
|
assert(handle, `No element found for selector: ${selector}`);
|
|
|
|
await handle.type(text, options);
|
2023-05-15 14:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated Replace with `new Promise(r => setTimeout(r, milliseconds));`.
|
|
|
|
*
|
|
|
|
* Causes your script to wait for the given number of milliseconds.
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
* It's generally recommended to not wait for a number of seconds, but instead
|
|
|
|
* use {@link Frame.waitForSelector}, {@link Frame.waitForXPath} or
|
|
|
|
* {@link Frame.waitForFunction} to wait for exactly the conditions you want.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* Wait for 1 second:
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* await frame.waitForTimeout(1000);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param milliseconds - the number of milliseconds to wait.
|
|
|
|
*/
|
2023-09-01 12:12:29 +00:00
|
|
|
async waitForTimeout(milliseconds: number): Promise<void> {
|
|
|
|
return await new Promise(resolve => {
|
2023-05-15 14:39:47 +00:00
|
|
|
setTimeout(resolve, milliseconds);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The frame's title.
|
|
|
|
*/
|
2023-08-31 14:39:32 +00:00
|
|
|
@throwIfDetached
|
2023-09-01 12:12:29 +00:00
|
|
|
async title(): Promise<string> {
|
2023-09-05 07:10:16 +00:00
|
|
|
return await this.isolatedRealm().evaluate(() => {
|
|
|
|
return document.title;
|
|
|
|
});
|
2023-05-15 14:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is typically coupled with an action that triggers a device
|
|
|
|
* request from an api such as WebBluetooth.
|
|
|
|
*
|
|
|
|
* :::caution
|
|
|
|
*
|
|
|
|
* This must be called before the device request is made. It will not return a
|
|
|
|
* currently active device prompt.
|
|
|
|
*
|
|
|
|
* :::
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const [devicePrompt] = Promise.all([
|
|
|
|
* frame.waitForDevicePrompt(),
|
|
|
|
* frame.click('#connect-bluetooth'),
|
|
|
|
* ]);
|
|
|
|
* await devicePrompt.select(
|
|
|
|
* await devicePrompt.waitForDevice(({name}) => name.includes('My Device'))
|
|
|
|
* );
|
|
|
|
* ```
|
2023-11-09 12:57:33 +00:00
|
|
|
*
|
|
|
|
* @internal
|
2023-05-15 14:39:47 +00:00
|
|
|
*/
|
2023-11-09 12:57:33 +00:00
|
|
|
abstract waitForDevicePrompt(
|
2023-05-15 14:39:47 +00:00
|
|
|
options?: WaitTimeoutOptions
|
|
|
|
): Promise<DeviceRequestPrompt>;
|
|
|
|
}
|