2017-06-17 21:27:51 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 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.
|
|
|
|
*/
|
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
import {Protocol} from 'devtools-protocol';
|
|
|
|
import {assert} from './assert.js';
|
2022-07-21 18:50:46 +00:00
|
|
|
import {CDPSession} from './Connection.js';
|
2022-07-06 07:05:37 +00:00
|
|
|
import {ElementHandle} from './ElementHandle.js';
|
2022-06-22 13:25:44 +00:00
|
|
|
import {EventEmitter} from './EventEmitter.js';
|
|
|
|
import {EVALUATION_SCRIPT_URL, ExecutionContext} from './ExecutionContext.js';
|
|
|
|
import {HTTPResponse} from './HTTPResponse.js';
|
|
|
|
import {MouseButton} from './Input.js';
|
2022-08-10 21:34:29 +00:00
|
|
|
import {
|
|
|
|
MAIN_WORLD,
|
|
|
|
IsolatedWorld,
|
|
|
|
IsolatedWorldChart,
|
|
|
|
PUPPETEER_WORLD,
|
|
|
|
WaitForSelectorOptions,
|
|
|
|
} from './IsolatedWorld.js';
|
2022-06-22 13:25:44 +00:00
|
|
|
import {LifecycleWatcher, PuppeteerLifeCycleEvent} from './LifecycleWatcher.js';
|
|
|
|
import {NetworkManager} from './NetworkManager.js';
|
|
|
|
import {Page} from './Page.js';
|
2022-07-21 18:50:46 +00:00
|
|
|
import {Target} from './Target.js';
|
2022-06-22 13:25:44 +00:00
|
|
|
import {TimeoutSettings} from './TimeoutSettings.js';
|
2022-07-06 07:05:37 +00:00
|
|
|
import {EvaluateFunc, HandleFor, NodeFor} from './types.js';
|
2022-07-25 09:20:38 +00:00
|
|
|
import {
|
|
|
|
createDeferredPromiseWithTimer,
|
|
|
|
debugError,
|
|
|
|
DeferredPromise,
|
|
|
|
isErrorLike,
|
|
|
|
} from './util.js';
|
2017-10-12 08:26:44 +00:00
|
|
|
|
2019-01-22 22:55:33 +00:00
|
|
|
const UTILITY_WORLD_NAME = '__puppeteer_utility_world__';
|
|
|
|
|
2020-07-08 10:00:11 +00:00
|
|
|
/**
|
|
|
|
* We use symbols to prevent external parties listening to these events.
|
|
|
|
* They are internal to Puppeteer.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export const FrameManagerEmittedEvents = {
|
|
|
|
FrameAttached: Symbol('FrameManager.FrameAttached'),
|
|
|
|
FrameNavigated: Symbol('FrameManager.FrameNavigated'),
|
|
|
|
FrameDetached: Symbol('FrameManager.FrameDetached'),
|
2022-03-09 11:24:17 +00:00
|
|
|
FrameSwapped: Symbol('FrameManager.FrameSwapped'),
|
2020-07-08 10:00:11 +00:00
|
|
|
LifecycleEvent: Symbol('FrameManager.LifecycleEvent'),
|
|
|
|
FrameNavigatedWithinDocument: Symbol(
|
|
|
|
'FrameManager.FrameNavigatedWithinDocument'
|
|
|
|
),
|
|
|
|
ExecutionContextCreated: Symbol('FrameManager.ExecutionContextCreated'),
|
|
|
|
ExecutionContextDestroyed: Symbol('FrameManager.ExecutionContextDestroyed'),
|
|
|
|
};
|
|
|
|
|
2020-07-02 15:13:22 +00:00
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* A frame manager manages the frames for a given {@link Page | page}.
|
|
|
|
*
|
2020-07-02 15:13:22 +00:00
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
export class FrameManager extends EventEmitter {
|
2022-06-13 09:16:25 +00:00
|
|
|
#page: Page;
|
|
|
|
#networkManager: NetworkManager;
|
|
|
|
#timeoutSettings: TimeoutSettings;
|
|
|
|
#frames = new Map<string, Frame>();
|
|
|
|
#contextIdToContext = new Map<string, ExecutionContext>();
|
|
|
|
#isolatedWorlds = new Set<string>();
|
|
|
|
#mainFrame?: Frame;
|
|
|
|
#client: CDPSession;
|
2022-07-25 09:20:38 +00:00
|
|
|
/**
|
|
|
|
* Keeps track of OOPIF targets/frames (target ID == frame ID for OOPIFs)
|
|
|
|
* that are being initialized.
|
|
|
|
*/
|
|
|
|
#framesPendingTargetInit = new Map<string, DeferredPromise<void>>();
|
|
|
|
/**
|
|
|
|
* Keeps track of frames that are in the process of being attached in #onFrameAttached.
|
|
|
|
*/
|
|
|
|
#framesPendingAttachment = new Map<string, DeferredPromise<void>>();
|
2022-06-13 09:16:25 +00:00
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
get timeoutSettings(): TimeoutSettings {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#timeoutSettings;
|
|
|
|
}
|
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
get networkManager(): NetworkManager {
|
|
|
|
return this.#networkManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
get client(): CDPSession {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#client;
|
|
|
|
}
|
2020-04-29 11:28:16 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
constructor(
|
|
|
|
client: CDPSession,
|
|
|
|
page: Page,
|
|
|
|
ignoreHTTPSErrors: boolean,
|
|
|
|
timeoutSettings: TimeoutSettings
|
|
|
|
) {
|
2017-06-21 20:51:06 +00:00
|
|
|
super();
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#client = client;
|
|
|
|
this.#page = page;
|
|
|
|
this.#networkManager = new NetworkManager(client, ignoreHTTPSErrors, this);
|
|
|
|
this.#timeoutSettings = timeoutSettings;
|
|
|
|
this.setupEventListeners(this.#client);
|
2021-10-28 09:25:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private setupEventListeners(session: CDPSession) {
|
2022-06-22 13:25:44 +00:00
|
|
|
session.on('Page.frameAttached', event => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onFrameAttached(session, event.frameId, event.parentFrameId);
|
2021-10-28 09:25:49 +00:00
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
session.on('Page.frameNavigated', event => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onFrameNavigated(event.frame);
|
2021-10-28 09:25:49 +00:00
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
session.on('Page.navigatedWithinDocument', event => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onFrameNavigatedWithinDocument(event.frameId, event.url);
|
2021-10-28 09:25:49 +00:00
|
|
|
});
|
|
|
|
session.on(
|
|
|
|
'Page.frameDetached',
|
|
|
|
(event: Protocol.Page.FrameDetachedEvent) => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onFrameDetached(
|
2021-10-28 09:25:49 +00:00
|
|
|
event.frameId,
|
|
|
|
event.reason as Protocol.Page.FrameDetachedEventReason
|
|
|
|
);
|
|
|
|
}
|
2020-09-21 11:23:36 +00:00
|
|
|
);
|
2022-06-22 13:25:44 +00:00
|
|
|
session.on('Page.frameStartedLoading', event => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onFrameStartedLoading(event.frameId);
|
2022-05-17 12:15:44 +00:00
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
session.on('Page.frameStoppedLoading', event => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onFrameStoppedLoading(event.frameId);
|
2021-10-28 09:25:49 +00:00
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
session.on('Runtime.executionContextCreated', event => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onExecutionContextCreated(event.context, session);
|
2021-10-28 09:25:49 +00:00
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
session.on('Runtime.executionContextDestroyed', event => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onExecutionContextDestroyed(event.executionContextId, session);
|
2021-10-28 09:25:49 +00:00
|
|
|
});
|
|
|
|
session.on('Runtime.executionContextsCleared', () => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onExecutionContextsCleared(session);
|
2021-10-28 09:25:49 +00:00
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
session.on('Page.lifecycleEvent', event => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onLifecycleEvent(event);
|
2021-10-28 09:25:49 +00:00
|
|
|
});
|
2019-04-10 04:42:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 09:20:38 +00:00
|
|
|
async initialize(
|
|
|
|
targetId: string,
|
|
|
|
client: CDPSession = this.#client
|
|
|
|
): Promise<void> {
|
2021-10-28 09:25:49 +00:00
|
|
|
try {
|
2022-07-25 09:20:38 +00:00
|
|
|
if (!this.#framesPendingTargetInit.has(targetId)) {
|
|
|
|
this.#framesPendingTargetInit.set(
|
|
|
|
targetId,
|
|
|
|
createDeferredPromiseWithTimer(
|
|
|
|
`Waiting for target frame ${targetId} failed`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2021-10-28 09:25:49 +00:00
|
|
|
const result = await Promise.all([
|
|
|
|
client.send('Page.enable'),
|
|
|
|
client.send('Page.getFrameTree'),
|
|
|
|
]);
|
2020-03-31 16:42:32 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
const {frameTree} = result[1];
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#handleFrameTree(client, frameTree);
|
2021-10-28 09:25:49 +00:00
|
|
|
await Promise.all([
|
2022-06-22 13:25:44 +00:00
|
|
|
client.send('Page.setLifecycleEventsEnabled', {enabled: true}),
|
2022-06-15 10:42:21 +00:00
|
|
|
client.send('Runtime.enable').then(() => {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.#createIsolatedWorld(client, UTILITY_WORLD_NAME);
|
2022-06-15 10:42:21 +00:00
|
|
|
}),
|
2021-10-28 09:25:49 +00:00
|
|
|
// TODO: Network manager is not aware of OOP iframes yet.
|
2022-06-13 09:16:25 +00:00
|
|
|
client === this.#client
|
|
|
|
? this.#networkManager.initialize()
|
2021-10-28 09:25:49 +00:00
|
|
|
: Promise.resolve(),
|
|
|
|
]);
|
|
|
|
} catch (error) {
|
|
|
|
// The target might have been closed before the initialization finished.
|
|
|
|
if (
|
2022-06-10 13:27:42 +00:00
|
|
|
isErrorLike(error) &&
|
2022-05-31 14:34:16 +00:00
|
|
|
(error.message.includes('Target closed') ||
|
|
|
|
error.message.includes('Session closed'))
|
2021-10-28 09:25:49 +00:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
2022-07-25 09:20:38 +00:00
|
|
|
} finally {
|
|
|
|
this.#framesPendingTargetInit.get(targetId)?.resolve();
|
|
|
|
this.#framesPendingTargetInit.delete(targetId);
|
2021-10-28 09:25:49 +00:00
|
|
|
}
|
2019-04-10 04:42:42 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
executionContextById(
|
|
|
|
contextId: number,
|
|
|
|
session: CDPSession = this.#client
|
|
|
|
): ExecutionContext {
|
|
|
|
const key = `${session.id()}:${contextId}`;
|
|
|
|
const context = this.#contextIdToContext.get(key);
|
|
|
|
assert(context, 'INTERNAL ERROR: missing context with id = ' + contextId);
|
|
|
|
return context;
|
2017-10-18 02:14:57 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
page(): Page {
|
|
|
|
return this.#page;
|
|
|
|
}
|
2022-08-10 15:59:58 +00:00
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
mainFrame(): Frame {
|
|
|
|
assert(this.#mainFrame, 'Requesting main frame too early!');
|
|
|
|
return this.#mainFrame;
|
|
|
|
}
|
2018-09-19 20:12:28 +00:00
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
frames(): Frame[] {
|
|
|
|
return Array.from(this.#frames.values());
|
2018-09-19 20:12:28 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
frame(frameId: string): Frame | null {
|
|
|
|
return this.#frames.get(frameId) || null;
|
2018-09-19 20:12:28 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
onAttachedToTarget(target: Target): void {
|
2022-07-21 18:50:46 +00:00
|
|
|
if (target._getTargetInfo().type !== 'iframe') {
|
2020-09-21 11:23:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:50:46 +00:00
|
|
|
const frame = this.#frames.get(target._getTargetInfo().targetId);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (frame) {
|
2022-08-10 21:34:29 +00:00
|
|
|
frame.updateClient(target._session()!);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2022-07-21 18:50:46 +00:00
|
|
|
this.setupEventListeners(target._session()!);
|
2022-07-25 09:20:38 +00:00
|
|
|
this.initialize(target._getTargetInfo().targetId, target._session());
|
2021-10-28 09:25:49 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
onDetachedFromTarget(target: Target): void {
|
2022-07-21 18:50:46 +00:00
|
|
|
const frame = this.#frames.get(target._targetId);
|
2021-10-28 09:25:49 +00:00
|
|
|
if (frame && frame.isOOPFrame()) {
|
|
|
|
// When an OOP iframe is removed from the page, it
|
|
|
|
// will only get a Target.detachedFromTarget event.
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#removeFramesRecursively(frame);
|
2021-10-28 09:25:49 +00:00
|
|
|
}
|
2020-09-21 11:23:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onLifecycleEvent(event: Protocol.Page.LifecycleEventEvent): void {
|
|
|
|
const frame = this.#frames.get(event.frameId);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!frame) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-10 23:33:14 +00:00
|
|
|
frame._onLifecycleEvent(event.loaderId, event.name);
|
2020-07-08 10:00:11 +00:00
|
|
|
this.emit(FrameManagerEmittedEvents.LifecycleEvent, frame);
|
2017-11-10 23:33:14 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onFrameStartedLoading(frameId: string): void {
|
|
|
|
const frame = this.#frames.get(frameId);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!frame) {
|
|
|
|
return;
|
|
|
|
}
|
2022-05-17 12:15:44 +00:00
|
|
|
frame._onLoadingStarted();
|
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onFrameStoppedLoading(frameId: string): void {
|
|
|
|
const frame = this.#frames.get(frameId);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!frame) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-10 22:59:41 +00:00
|
|
|
frame._onLoadingStopped();
|
2020-07-08 10:00:11 +00:00
|
|
|
this.emit(FrameManagerEmittedEvents.LifecycleEvent, frame);
|
2018-04-10 22:59:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#handleFrameTree(
|
2021-10-28 09:25:49 +00:00
|
|
|
session: CDPSession,
|
|
|
|
frameTree: Protocol.Page.FrameTree
|
|
|
|
): void {
|
|
|
|
if (frameTree.frame.parentId) {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onFrameAttached(
|
2021-10-28 09:25:49 +00:00
|
|
|
session,
|
|
|
|
frameTree.frame.id,
|
|
|
|
frameTree.frame.parentId
|
|
|
|
);
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onFrameNavigated(frameTree.frame);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!frameTree.childFrames) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-18 02:14:57 +00:00
|
|
|
|
2021-10-28 09:25:49 +00:00
|
|
|
for (const child of frameTree.childFrames) {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#handleFrameTree(session, child);
|
2021-10-28 09:25:49 +00:00
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onFrameAttached(
|
2021-10-28 09:25:49 +00:00
|
|
|
session: CDPSession,
|
|
|
|
frameId: string,
|
2022-07-25 09:20:38 +00:00
|
|
|
parentFrameId: string
|
2021-10-28 09:25:49 +00:00
|
|
|
): void {
|
2022-06-13 09:16:25 +00:00
|
|
|
if (this.#frames.has(frameId)) {
|
|
|
|
const frame = this.#frames.get(frameId)!;
|
2021-10-28 09:25:49 +00:00
|
|
|
if (session && frame.isOOPFrame()) {
|
|
|
|
// If an OOP iframes becomes a normal iframe again
|
|
|
|
// it is first attached to the parent page before
|
|
|
|
// the target is removed.
|
2022-08-10 21:34:29 +00:00
|
|
|
frame.updateClient(session);
|
2021-10-28 09:25:49 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
const parentFrame = this.#frames.get(parentFrameId);
|
2022-07-25 09:20:38 +00:00
|
|
|
|
|
|
|
const complete = (parentFrame: Frame) => {
|
|
|
|
assert(parentFrame, `Parent frame ${parentFrameId} not found`);
|
|
|
|
const frame = new Frame(this, parentFrame, frameId, session);
|
|
|
|
this.#frames.set(frame._id, frame);
|
|
|
|
this.emit(FrameManagerEmittedEvents.FrameAttached, frame);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (parentFrame) {
|
|
|
|
return complete(parentFrame);
|
|
|
|
}
|
|
|
|
|
2022-08-09 11:29:12 +00:00
|
|
|
const frame = this.#framesPendingTargetInit.get(parentFrameId);
|
|
|
|
if (frame) {
|
2022-07-25 09:20:38 +00:00
|
|
|
if (!this.#framesPendingAttachment.has(frameId)) {
|
|
|
|
this.#framesPendingAttachment.set(
|
|
|
|
frameId,
|
|
|
|
createDeferredPromiseWithTimer(
|
|
|
|
`Waiting for frame ${frameId} to attach failed`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2022-08-09 11:29:12 +00:00
|
|
|
frame.then(() => {
|
2022-07-25 09:20:38 +00:00
|
|
|
complete(this.#frames.get(parentFrameId)!);
|
|
|
|
this.#framesPendingAttachment.get(frameId)?.resolve();
|
|
|
|
this.#framesPendingAttachment.delete(frameId);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`Parent frame ${parentFrameId} not found`);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onFrameNavigated(framePayload: Protocol.Page.Frame): void {
|
2022-07-25 09:20:38 +00:00
|
|
|
const frameId = framePayload.id;
|
2017-07-28 23:52:38 +00:00
|
|
|
const isMainFrame = !framePayload.parentId;
|
2022-07-25 09:20:38 +00:00
|
|
|
const frame = isMainFrame ? this.#mainFrame : this.#frames.get(frameId);
|
2017-07-28 23:52:38 +00:00
|
|
|
|
2022-07-25 09:20:38 +00:00
|
|
|
const complete = (frame?: Frame) => {
|
|
|
|
assert(
|
|
|
|
isMainFrame || frame,
|
|
|
|
`Missing frame isMainFrame=${isMainFrame}, frameId=${frameId}`
|
|
|
|
);
|
2017-07-28 23:52:38 +00:00
|
|
|
|
2022-07-25 09:20:38 +00:00
|
|
|
// Detach all child frames first.
|
2017-07-28 23:52:38 +00:00
|
|
|
if (frame) {
|
2022-07-25 09:20:38 +00:00
|
|
|
for (const child of frame.childFrames()) {
|
|
|
|
this.#removeFramesRecursively(child);
|
|
|
|
}
|
2017-07-28 23:52:38 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 09:20:38 +00:00
|
|
|
// Update or create main frame.
|
|
|
|
if (isMainFrame) {
|
|
|
|
if (frame) {
|
|
|
|
// Update frame id to retain frame identity on cross-process navigation.
|
|
|
|
this.#frames.delete(frame._id);
|
|
|
|
frame._id = frameId;
|
|
|
|
} else {
|
|
|
|
// Initial main frame navigation.
|
|
|
|
frame = new Frame(this, null, frameId, this.#client);
|
|
|
|
}
|
|
|
|
this.#frames.set(frameId, frame);
|
|
|
|
this.#mainFrame = frame;
|
|
|
|
}
|
2017-07-28 23:52:38 +00:00
|
|
|
|
2022-07-25 09:20:38 +00:00
|
|
|
// Update frame payload.
|
|
|
|
assert(frame);
|
|
|
|
frame._navigated(framePayload);
|
|
|
|
|
|
|
|
this.emit(FrameManagerEmittedEvents.FrameNavigated, frame);
|
|
|
|
};
|
2022-08-09 11:29:12 +00:00
|
|
|
const pendingFrame = this.#framesPendingAttachment.get(frameId);
|
|
|
|
if (pendingFrame) {
|
|
|
|
pendingFrame.then(() => {
|
2022-07-25 09:20:38 +00:00
|
|
|
complete(isMainFrame ? this.#mainFrame : this.#frames.get(frameId));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
complete(frame);
|
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
async #createIsolatedWorld(session: CDPSession, name: string): Promise<void> {
|
2021-10-28 09:25:49 +00:00
|
|
|
const key = `${session.id()}:${name}`;
|
2022-08-10 21:34:29 +00:00
|
|
|
|
2022-06-14 11:55:35 +00:00
|
|
|
if (this.#isolatedWorlds.has(key)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-10-28 09:25:49 +00:00
|
|
|
|
|
|
|
await session.send('Page.addScriptToEvaluateOnNewDocument', {
|
2019-01-22 22:55:33 +00:00
|
|
|
source: `//# sourceURL=${EVALUATION_SCRIPT_URL}`,
|
|
|
|
worldName: name,
|
2020-11-26 11:43:42 +00:00
|
|
|
});
|
2022-08-10 21:34:29 +00:00
|
|
|
|
2020-11-26 11:43:42 +00:00
|
|
|
await Promise.all(
|
2021-10-28 09:25:49 +00:00
|
|
|
this.frames()
|
2022-06-22 13:25:44 +00:00
|
|
|
.filter(frame => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return frame._client() === session;
|
|
|
|
})
|
2022-06-22 13:25:44 +00:00
|
|
|
.map(frame => {
|
2022-08-10 21:34:29 +00:00
|
|
|
// Frames might be removed before we send this, so we don't want to
|
|
|
|
// throw an error.
|
2022-06-15 10:42:21 +00:00
|
|
|
return session
|
2022-03-02 12:03:44 +00:00
|
|
|
.send('Page.createIsolatedWorld', {
|
|
|
|
frameId: frame._id,
|
|
|
|
worldName: name,
|
|
|
|
grantUniveralAccess: true,
|
|
|
|
})
|
2022-06-15 10:42:21 +00:00
|
|
|
.catch(debugError);
|
|
|
|
})
|
2020-11-26 11:43:42 +00:00
|
|
|
);
|
2022-08-10 21:34:29 +00:00
|
|
|
|
|
|
|
this.#isolatedWorlds.add(key);
|
2019-01-22 22:55:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onFrameNavigatedWithinDocument(frameId: string, url: string): void {
|
|
|
|
const frame = this.#frames.get(frameId);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!frame) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-10 06:38:20 +00:00
|
|
|
frame._navigatedWithinDocument(url);
|
2020-07-08 10:00:11 +00:00
|
|
|
this.emit(FrameManagerEmittedEvents.FrameNavigatedWithinDocument, frame);
|
|
|
|
this.emit(FrameManagerEmittedEvents.FrameNavigated, frame);
|
2018-04-10 06:38:20 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onFrameDetached(
|
2021-10-28 09:25:49 +00:00
|
|
|
frameId: string,
|
|
|
|
reason: Protocol.Page.FrameDetachedEventReason
|
|
|
|
): void {
|
2022-06-13 09:16:25 +00:00
|
|
|
const frame = this.#frames.get(frameId);
|
2021-10-28 09:25:49 +00:00
|
|
|
if (reason === 'remove') {
|
|
|
|
// Only remove the frame if the reason for the detached event is
|
|
|
|
// an actual removement of the frame.
|
|
|
|
// For frames that become OOP iframes, the reason would be 'swap'.
|
2022-06-14 11:55:35 +00:00
|
|
|
if (frame) {
|
|
|
|
this.#removeFramesRecursively(frame);
|
|
|
|
}
|
2022-03-09 11:24:17 +00:00
|
|
|
} else if (reason === 'swap') {
|
|
|
|
this.emit(FrameManagerEmittedEvents.FrameSwapped, frame);
|
2021-10-28 09:25:49 +00:00
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onExecutionContextCreated(
|
2021-10-28 09:25:49 +00:00
|
|
|
contextPayload: Protocol.Runtime.ExecutionContextDescription,
|
|
|
|
session: CDPSession
|
2020-05-07 10:54:55 +00:00
|
|
|
): void {
|
2022-06-22 13:25:44 +00:00
|
|
|
const auxData = contextPayload.auxData as {frameId?: string} | undefined;
|
2022-05-31 14:34:16 +00:00
|
|
|
const frameId = auxData && auxData.frameId;
|
|
|
|
const frame =
|
2022-06-13 09:16:25 +00:00
|
|
|
typeof frameId === 'string' ? this.#frames.get(frameId) : undefined;
|
2022-08-09 13:17:42 +00:00
|
|
|
let world: IsolatedWorld | undefined;
|
2019-01-22 22:55:33 +00:00
|
|
|
if (frame) {
|
2021-10-28 09:25:49 +00:00
|
|
|
// Only care about execution contexts created for the current session.
|
2022-06-14 11:55:35 +00:00
|
|
|
if (frame._client() !== session) {
|
|
|
|
return;
|
|
|
|
}
|
2021-10-28 09:25:49 +00:00
|
|
|
|
2019-04-11 20:26:18 +00:00
|
|
|
if (contextPayload.auxData && !!contextPayload.auxData['isDefault']) {
|
2022-08-10 21:34:29 +00:00
|
|
|
world = frame.worlds[MAIN_WORLD];
|
2020-05-07 10:54:55 +00:00
|
|
|
} else if (
|
|
|
|
contextPayload.name === UTILITY_WORLD_NAME &&
|
2022-08-10 21:34:29 +00:00
|
|
|
!frame.worlds[PUPPETEER_WORLD].hasContext()
|
2020-05-07 10:54:55 +00:00
|
|
|
) {
|
2019-04-11 20:26:18 +00:00
|
|
|
// In case of multiple sessions to the same target, there's a race between
|
|
|
|
// connections so we might end up creating multiple isolated worlds.
|
|
|
|
// We can use either.
|
2022-08-10 21:34:29 +00:00
|
|
|
world = frame.worlds[PUPPETEER_WORLD];
|
2019-04-11 20:26:18 +00:00
|
|
|
}
|
2019-01-22 22:55:33 +00:00
|
|
|
}
|
2021-10-28 09:25:49 +00:00
|
|
|
const context = new ExecutionContext(
|
2022-06-13 09:16:25 +00:00
|
|
|
frame?._client() || this.#client,
|
2021-10-28 09:25:49 +00:00
|
|
|
contextPayload,
|
|
|
|
world
|
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (world) {
|
2022-08-09 11:29:12 +00:00
|
|
|
world.setContext(context);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2021-10-28 09:25:49 +00:00
|
|
|
const key = `${session.id()}:${contextPayload.id}`;
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#contextIdToContext.set(key, context);
|
2017-11-19 00:27:52 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onExecutionContextDestroyed(
|
2021-10-28 09:25:49 +00:00
|
|
|
executionContextId: number,
|
|
|
|
session: CDPSession
|
|
|
|
): void {
|
|
|
|
const key = `${session.id()}:${executionContextId}`;
|
2022-06-13 09:16:25 +00:00
|
|
|
const context = this.#contextIdToContext.get(key);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!context) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#contextIdToContext.delete(key);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (context._world) {
|
2022-08-09 11:29:12 +00:00
|
|
|
context._world.clearContext();
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onExecutionContextsCleared(session: CDPSession): void {
|
|
|
|
for (const [key, context] of this.#contextIdToContext.entries()) {
|
2021-10-28 09:25:49 +00:00
|
|
|
// Make sure to only clear execution contexts that belong
|
|
|
|
// to the current session.
|
2022-06-14 11:55:35 +00:00
|
|
|
if (context._client !== session) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (context._world) {
|
2022-08-09 11:29:12 +00:00
|
|
|
context._world.clearContext();
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#contextIdToContext.delete(key);
|
2018-08-09 21:57:08 +00:00
|
|
|
}
|
2017-10-06 22:35:02 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#removeFramesRecursively(frame: Frame): void {
|
2022-06-14 11:55:35 +00:00
|
|
|
for (const child of frame.childFrames()) {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#removeFramesRecursively(child);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
frame._detach();
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#frames.delete(frame._id);
|
2020-07-08 10:00:11 +00:00
|
|
|
this.emit(FrameManagerEmittedEvents.FrameDetached, frame);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-17 21:27:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-02 15:13:22 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2020-07-09 13:22:58 +00:00
|
|
|
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?: string | 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface FrameAddScriptTagOptions {
|
|
|
|
/**
|
|
|
|
* the URL of the script to be added.
|
|
|
|
*/
|
|
|
|
url?: string;
|
|
|
|
/**
|
|
|
|
* The 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;
|
|
|
|
/**
|
|
|
|
* Raw JavaScript content to be injected into the frame.
|
|
|
|
*/
|
|
|
|
content?: string;
|
|
|
|
/**
|
|
|
|
* Set the script's `type`. Use `module` in order to load an ES2015 module.
|
|
|
|
*/
|
|
|
|
type?: 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-08-16 06:22:45 +00:00
|
|
|
* Represents a DOM frame.
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
2022-08-16 06:22:45 +00:00
|
|
|
* 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.
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
2022-08-16 06:22:45 +00:00
|
|
|
* @example
|
|
|
|
* At any point in time, {@link Page | pages} expose their current frame
|
|
|
|
* tree via the {@link Page.mainFrame} and {@link Frame.childFrames} methods.
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* @example
|
2020-07-09 13:22:58 +00:00
|
|
|
* An example of dumping frame tree:
|
|
|
|
*
|
2022-07-01 11:52:39 +00:00
|
|
|
* ```ts
|
2020-07-09 13:22:58 +00:00
|
|
|
* const puppeteer = require('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()) {
|
2022-08-12 12:15:26 +00:00
|
|
|
* dumpFrameTree(child, indent + ' ');
|
2020-07-09 13:22:58 +00:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* })();
|
|
|
|
* ```
|
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* @example
|
2020-07-09 13:22:58 +00:00
|
|
|
* An example of getting text from an iframe element:
|
|
|
|
*
|
2022-07-01 11:52:39 +00:00
|
|
|
* ```ts
|
2020-07-09 13:22:58 +00:00
|
|
|
* const frame = page.frames().find(frame => frame.name() === 'myframe');
|
|
|
|
* const text = await frame.$eval('.selector', element => element.textContent);
|
|
|
|
* console.log(text);
|
|
|
|
* ```
|
|
|
|
*
|
2022-08-16 06:22:45 +00:00
|
|
|
* @remarks
|
|
|
|
* Frame lifecycles are controlled by three events that are all dispatched on
|
|
|
|
* the parent {@link Frame.page | page}:
|
|
|
|
*
|
|
|
|
* - {@link PageEmittedEvents.FrameAttached}
|
|
|
|
* - {@link PageEmittedEvents.FrameNavigated}
|
|
|
|
* - {@link PageEmittedEvents.FrameDetached}
|
|
|
|
*
|
2020-07-09 13:22:58 +00:00
|
|
|
* @public
|
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
export class Frame {
|
2022-06-13 09:16:25 +00:00
|
|
|
#parentFrame: Frame | null;
|
|
|
|
#url = '';
|
|
|
|
#detached = false;
|
|
|
|
#client!: CDPSession;
|
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
worlds!: IsolatedWorldChart;
|
2020-07-02 15:13:22 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
_frameManager: FrameManager;
|
2020-07-02 15:13:22 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
_id: string;
|
2020-07-02 15:13:22 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
_loaderId = '';
|
2020-07-02 15:13:22 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
_name?: string;
|
2022-05-17 12:15:44 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_hasStartedLoading = false;
|
2020-07-02 15:13:22 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
_lifecycleEvents = new Set<string>();
|
2020-07-02 15:13:22 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
_childFrames: Set<Frame>;
|
|
|
|
|
2020-07-02 15:13:22 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
constructor(
|
|
|
|
frameManager: FrameManager,
|
|
|
|
parentFrame: Frame | null,
|
2021-10-28 09:25:49 +00:00
|
|
|
frameId: string,
|
|
|
|
client: CDPSession
|
2020-05-07 10:54:55 +00:00
|
|
|
) {
|
2018-08-16 23:16:27 +00:00
|
|
|
this._frameManager = frameManager;
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#parentFrame = parentFrame ?? null;
|
|
|
|
this.#url = '';
|
2017-06-21 20:51:06 +00:00
|
|
|
this._id = frameId;
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#detached = false;
|
2017-11-19 00:27:52 +00:00
|
|
|
|
2017-11-10 23:33:14 +00:00
|
|
|
this._loaderId = '';
|
2021-10-28 09:25:49 +00:00
|
|
|
|
|
|
|
this._childFrames = new Set();
|
2022-06-14 11:55:35 +00:00
|
|
|
if (this.#parentFrame) {
|
|
|
|
this.#parentFrame._childFrames.add(this);
|
|
|
|
}
|
2021-10-28 09:25:49 +00:00
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
this.updateClient(client);
|
2021-10-28 09:25:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-08-10 21:34:29 +00:00
|
|
|
updateClient(client: CDPSession): void {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#client = client;
|
2022-08-10 21:34:29 +00:00
|
|
|
this.worlds = {
|
|
|
|
[MAIN_WORLD]: new IsolatedWorld(
|
|
|
|
client,
|
|
|
|
this._frameManager,
|
|
|
|
this,
|
|
|
|
this._frameManager.timeoutSettings
|
|
|
|
),
|
|
|
|
[PUPPETEER_WORLD]: new IsolatedWorld(
|
|
|
|
client,
|
|
|
|
this._frameManager,
|
|
|
|
this,
|
|
|
|
this._frameManager.timeoutSettings
|
|
|
|
),
|
|
|
|
};
|
2022-07-11 19:06:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* @returns The page associated with the frame.
|
2022-07-11 19:06:35 +00:00
|
|
|
*/
|
|
|
|
page(): Page {
|
|
|
|
return this._frameManager.page();
|
2021-10-28 09:25:49 +00:00
|
|
|
}
|
2017-07-20 02:04:51 +00:00
|
|
|
|
2022-02-21 14:58:37 +00:00
|
|
|
/**
|
2022-08-16 06:22:45 +00:00
|
|
|
* @returns `true` if the frame is an out-of-process (OOP) frame. Otherwise,
|
|
|
|
* `false`.
|
2022-02-21 14:58:37 +00:00
|
|
|
*/
|
2021-10-28 09:25:49 +00:00
|
|
|
isOOPFrame(): boolean {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.#client !== this._frameManager.client;
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* Navigates a frame to the given url.
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* @remarks
|
|
|
|
* Navigation to `about:blank` or navigation to the same URL with a different
|
|
|
|
* hash will succeed and return `null`.
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* :::warning
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* Headless mode doesn't support navigation to a PDF document. See the {@link
|
|
|
|
* https://bugs.chromium.org/p/chromium/issues/detail?id=761295 | upstream
|
2020-07-09 13:22:58 +00:00
|
|
|
* issue}.
|
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* :::
|
|
|
|
*
|
2020-07-09 13:22:58 +00:00
|
|
|
* @param url - the URL to navigate the frame to. This should include the
|
|
|
|
* scheme, e.g. `https://`.
|
|
|
|
* @param options - navigation options. `waitUntil` is useful to define when
|
|
|
|
* the navigation should be considered successful - see the docs for
|
|
|
|
* {@link PuppeteerLifeCycleEvent} for more details.
|
|
|
|
*
|
|
|
|
* @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.
|
2022-08-10 21:34:29 +00:00
|
|
|
* @throws This method will throw an error if:
|
|
|
|
*
|
|
|
|
* - there's an SSL error (e.g. in case of self-signed certificates).
|
|
|
|
* - target URL is invalid.
|
|
|
|
* - the `timeout` is exceeded during navigation.
|
|
|
|
* - 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
|
2022-08-12 12:15:26 +00:00
|
|
|
* Server Error". The status code for such responses can be retrieved by
|
2022-08-10 21:34:29 +00:00
|
|
|
* calling {@link HTTPResponse.status}.
|
2020-07-09 13:22:58 +00:00
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
async goto(
|
|
|
|
url: string,
|
|
|
|
options: {
|
|
|
|
referer?: string;
|
|
|
|
timeout?: number;
|
|
|
|
waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[];
|
2020-06-18 11:44:46 +00:00
|
|
|
} = {}
|
2020-05-29 10:49:30 +00:00
|
|
|
): Promise<HTTPResponse | null> {
|
2022-08-10 21:34:29 +00:00
|
|
|
assertNoLegacyNavigationOptions(options);
|
|
|
|
const {
|
|
|
|
referer = this._frameManager.networkManager.extraHTTPHeaders()['referer'],
|
|
|
|
waitUntil = ['load'],
|
|
|
|
timeout = this._frameManager.timeoutSettings.navigationTimeout(),
|
|
|
|
} = options;
|
|
|
|
|
|
|
|
let ensureNewDocumentNavigation = false;
|
|
|
|
const watcher = new LifecycleWatcher(
|
|
|
|
this._frameManager,
|
|
|
|
this,
|
|
|
|
waitUntil,
|
|
|
|
timeout
|
|
|
|
);
|
|
|
|
let error = await Promise.race([
|
|
|
|
navigate(this.#client, url, referer, this._id),
|
|
|
|
watcher.timeoutOrTerminationPromise(),
|
|
|
|
]);
|
|
|
|
if (!error) {
|
|
|
|
error = await Promise.race([
|
|
|
|
watcher.timeoutOrTerminationPromise(),
|
|
|
|
ensureNewDocumentNavigation
|
|
|
|
? watcher.newDocumentNavigationPromise()
|
|
|
|
: watcher.sameDocumentNavigationPromise(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return await watcher.navigationResponse();
|
|
|
|
} finally {
|
|
|
|
watcher.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function navigate(
|
|
|
|
client: CDPSession,
|
|
|
|
url: string,
|
|
|
|
referrer: string | undefined,
|
|
|
|
frameId: string
|
|
|
|
): Promise<Error | null> {
|
|
|
|
try {
|
|
|
|
const response = await client.send('Page.navigate', {
|
|
|
|
url,
|
|
|
|
referrer,
|
|
|
|
frameId,
|
|
|
|
});
|
|
|
|
ensureNewDocumentNavigation = !!response.loaderId;
|
|
|
|
return response.errorText
|
|
|
|
? new Error(`${response.errorText} at ${url}`)
|
|
|
|
: null;
|
|
|
|
} catch (error) {
|
|
|
|
if (isErrorLike(error)) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2018-09-20 18:31:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* Waits for the frame to navigate. It is useful for when you run code which
|
|
|
|
* will indirectly cause the frame to navigate.
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* 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.
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* @example
|
2022-08-12 12:15:26 +00:00
|
|
|
*
|
2022-07-01 11:52:39 +00:00
|
|
|
* ```ts
|
2020-07-09 13:22:58 +00:00
|
|
|
* 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'),
|
|
|
|
* ]);
|
|
|
|
* ```
|
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* @param options - options to configure when the navigation is consided
|
|
|
|
* finished.
|
2020-07-09 13:22:58 +00:00
|
|
|
* @returns a promise that resolves when the frame navigates to a new URL.
|
|
|
|
*/
|
2020-06-23 05:18:46 +00:00
|
|
|
async waitForNavigation(
|
|
|
|
options: {
|
|
|
|
timeout?: number;
|
|
|
|
waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[];
|
|
|
|
} = {}
|
|
|
|
): Promise<HTTPResponse | null> {
|
2022-08-10 21:34:29 +00:00
|
|
|
assertNoLegacyNavigationOptions(options);
|
|
|
|
const {
|
|
|
|
waitUntil = ['load'],
|
|
|
|
timeout = this._frameManager.timeoutSettings.navigationTimeout(),
|
|
|
|
} = options;
|
|
|
|
const watcher = new LifecycleWatcher(
|
|
|
|
this._frameManager,
|
|
|
|
this,
|
|
|
|
waitUntil,
|
|
|
|
timeout
|
|
|
|
);
|
|
|
|
const error = await Promise.race([
|
|
|
|
watcher.timeoutOrTerminationPromise(),
|
|
|
|
watcher.sameDocumentNavigationPromise(),
|
|
|
|
watcher.newDocumentNavigationPromise(),
|
|
|
|
]);
|
|
|
|
try {
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return await watcher.navigationResponse();
|
|
|
|
} finally {
|
|
|
|
watcher.dispose();
|
|
|
|
}
|
2017-10-06 22:35:02 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_client(): CDPSession {
|
|
|
|
return this.#client;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-08-16 06:22:45 +00:00
|
|
|
* @deprecated Do not use the execution context directly.
|
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* @returns a promise that resolves to the frame's default execution context.
|
|
|
|
*/
|
|
|
|
executionContext(): Promise<ExecutionContext> {
|
|
|
|
return this.worlds[MAIN_WORLD].executionContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Behaves identically to {@link Page.evaluateHandle} except it's run within
|
|
|
|
* the context of this frame.
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
2022-08-16 06:22:45 +00:00
|
|
|
* @see {@link Page.evaluateHandle} for details.
|
2020-07-09 13:22:58 +00:00
|
|
|
*/
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
async evaluateHandle<
|
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>
|
|
|
|
>(
|
|
|
|
pageFunction: Func | string,
|
2022-06-24 06:40:08 +00:00
|
|
|
...args: Params
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
): Promise<HandleFor<Awaited<ReturnType<Func>>>> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.worlds[MAIN_WORLD].evaluateHandle(pageFunction, ...args);
|
2018-01-25 05:16:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* Behaves identically to {@link Page.evaluate} except it's run within the
|
|
|
|
* the context of this frame.
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
2022-08-16 06:22:45 +00:00
|
|
|
* @see {@link Page.evaluate} for details.
|
2020-07-09 13:22:58 +00:00
|
|
|
*/
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
async evaluate<
|
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>
|
|
|
|
>(
|
|
|
|
pageFunction: Func | string,
|
2022-06-24 06:40:08 +00:00
|
|
|
...args: Params
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
): Promise<Awaited<ReturnType<Func>>> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.worlds[MAIN_WORLD].evaluate(pageFunction, ...args);
|
2017-08-15 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* Queries the frame for an element matching the given selector.
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* @param selector - The selector to query for.
|
|
|
|
* @returns A {@link ElementHandle | element handle} to the first element
|
|
|
|
* matching the given selector. Otherwise, `null`.
|
2020-07-09 13:22:58 +00:00
|
|
|
*/
|
2022-07-06 07:05:37 +00:00
|
|
|
async $<Selector extends string>(
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
selector: Selector
|
2022-07-06 07:05:37 +00:00
|
|
|
): Promise<ElementHandle<NodeFor<Selector>> | null> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.worlds[MAIN_WORLD].$(selector);
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* Queries the frame for all elements matching the given selector.
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* @param selector - The selector to query for.
|
|
|
|
* @returns An array of {@link ElementHandle | element handles} that point to
|
|
|
|
* elements matching the given selector.
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
*/
|
2022-07-06 07:05:37 +00:00
|
|
|
async $$<Selector extends string>(
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
selector: Selector
|
2022-07-06 07:05:37 +00:00
|
|
|
): Promise<Array<ElementHandle<NodeFor<Selector>>>> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.worlds[MAIN_WORLD].$$(selector);
|
2017-12-20 00:23:45 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* Runs the given function on the first element matching the given selector in
|
|
|
|
* the frame.
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* If the given function returns a promise, then this method will wait till
|
|
|
|
* the promise resolves.
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
|
|
|
* @example
|
2022-08-12 12:15:26 +00:00
|
|
|
*
|
2022-07-01 11:52:39 +00:00
|
|
|
* ```ts
|
2020-07-09 13:22:58 +00:00
|
|
|
* const searchValue = await frame.$eval('#search', el => el.value);
|
|
|
|
* ```
|
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* @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.
|
2020-07-09 13:22:58 +00:00
|
|
|
*/
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
async $eval<
|
2022-07-06 07:05:37 +00:00
|
|
|
Selector extends string,
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFunc<
|
2022-07-06 07:05:37 +00:00
|
|
|
[ElementHandle<NodeFor<Selector>>, ...Params]
|
|
|
|
> = EvaluateFunc<[ElementHandle<NodeFor<Selector>>, ...Params]>
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
>(
|
|
|
|
selector: Selector,
|
|
|
|
pageFunction: Func | string,
|
2022-06-24 06:40:08 +00:00
|
|
|
...args: Params
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
): Promise<Awaited<ReturnType<Func>>> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.worlds[MAIN_WORLD].$eval(selector, pageFunction, ...args);
|
2017-08-31 22:38:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* Runs the given function on an array of elements matching the given selector
|
|
|
|
* in the frame.
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* If the given function returns a promise, then this method will wait till
|
|
|
|
* the promise resolves.
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
|
|
|
* @example
|
2022-08-12 12:15:26 +00:00
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* ```js
|
2020-07-09 13:22:58 +00:00
|
|
|
* const divsCounts = await frame.$$eval('div', divs => divs.length);
|
|
|
|
* ```
|
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* @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.
|
2020-07-09 13:22:58 +00:00
|
|
|
*/
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
async $$eval<
|
2022-07-06 07:05:37 +00:00
|
|
|
Selector extends string,
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFunc<
|
2022-07-06 07:05:37 +00:00
|
|
|
[Array<NodeFor<Selector>>, ...Params]
|
|
|
|
> = EvaluateFunc<[Array<NodeFor<Selector>>, ...Params]>
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
>(
|
|
|
|
selector: Selector,
|
|
|
|
pageFunction: Func | string,
|
2022-06-24 06:40:08 +00:00
|
|
|
...args: Params
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
): Promise<Awaited<ReturnType<Func>>> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.worlds[MAIN_WORLD].$$eval(selector, pageFunction, ...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated Use {@link Frame.$$} with the `xpath` prefix.
|
|
|
|
*
|
|
|
|
* This method evaluates the given XPath expression and returns the results.
|
|
|
|
* @param expression - the XPath expression to evaluate.
|
|
|
|
*/
|
|
|
|
async $x(expression: string): Promise<Array<ElementHandle<Node>>> {
|
|
|
|
return this.worlds[MAIN_WORLD].$x(expression);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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.
|
|
|
|
*
|
|
|
|
* @example
|
2022-08-12 12:15:26 +00:00
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* ```ts
|
|
|
|
* const puppeteer = require('puppeteer');
|
|
|
|
*
|
|
|
|
* (async () => {
|
|
|
|
* const browser = await puppeteer.launch();
|
|
|
|
* const page = await browser.newPage();
|
|
|
|
* let currentURL;
|
2022-08-12 12:15:26 +00:00
|
|
|
* page
|
|
|
|
* .mainFrame()
|
|
|
|
* .waitForSelector('img')
|
|
|
|
* .then(() => console.log('First URL with image: ' + currentURL));
|
2022-08-10 21:34:29 +00:00
|
|
|
*
|
2022-08-12 12:15:26 +00:00
|
|
|
* for (currentURL of [
|
|
|
|
* 'https://example.com',
|
|
|
|
* 'https://google.com',
|
|
|
|
* 'https://bbc.com',
|
|
|
|
* ]) {
|
2022-08-10 21:34:29 +00:00
|
|
|
* await page.goto(currentURL);
|
|
|
|
* }
|
|
|
|
* await browser.close();
|
|
|
|
* })();
|
|
|
|
* ```
|
2022-08-12 12:15:26 +00:00
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
async waitForSelector<Selector extends string>(
|
|
|
|
selector: Selector,
|
|
|
|
options: WaitForSelectorOptions = {}
|
|
|
|
): Promise<ElementHandle<NodeFor<Selector>> | null> {
|
|
|
|
const handle = await this.worlds[PUPPETEER_WORLD].waitForSelector(
|
|
|
|
selector,
|
|
|
|
options
|
|
|
|
);
|
|
|
|
if (!handle) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const mainHandle = (await this.worlds[MAIN_WORLD].adoptHandle(
|
|
|
|
handle
|
|
|
|
)) as ElementHandle<NodeFor<Selector>>;
|
|
|
|
await handle.dispose();
|
|
|
|
return mainHandle;
|
2017-08-23 05:56:55 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* @deprecated Use {@link Frame.waitForSelector} with the `xpath` prefix.
|
|
|
|
*
|
|
|
|
* 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.
|
2022-08-12 12:15:26 +00:00
|
|
|
* @param options - options to configure the visiblity of the element and how
|
2022-08-10 21:34:29 +00:00
|
|
|
* long to wait before timing out.
|
|
|
|
*/
|
|
|
|
async waitForXPath(
|
|
|
|
xpath: string,
|
|
|
|
options: WaitForSelectorOptions = {}
|
|
|
|
): Promise<ElementHandle<Node> | null> {
|
|
|
|
if (xpath.startsWith('//')) {
|
|
|
|
xpath = `.${xpath}`;
|
|
|
|
}
|
|
|
|
return this.waitForSelector(`xpath/${xpath}`, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @example
|
|
|
|
* The `waitForFunction` can be used to observe viewport size change:
|
2022-08-12 12:15:26 +00:00
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* ```ts
|
|
|
|
* const puppeteer = require('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
|
2022-08-12 12:15:26 +00:00
|
|
|
* );
|
2022-08-10 21:34:29 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
waitForFunction<
|
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>
|
|
|
|
>(
|
|
|
|
pageFunction: Func | string,
|
|
|
|
options: FrameWaitForFunctionOptions = {},
|
|
|
|
...args: Params
|
|
|
|
): Promise<HandleFor<Awaited<ReturnType<Func>>>> {
|
|
|
|
// TODO: Fix when NodeHandle has been added.
|
|
|
|
return this.worlds[MAIN_WORLD].waitForFunction(
|
|
|
|
pageFunction,
|
|
|
|
options,
|
|
|
|
...args
|
|
|
|
) as Promise<HandleFor<Awaited<ReturnType<Func>>>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns The full HTML contents of the frame, including the DOCTYPE.
|
2020-07-09 13:22:58 +00:00
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
async content(): Promise<string> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.worlds[PUPPETEER_WORLD].content();
|
2017-11-23 02:44:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
|
|
|
* Set the content of the frame.
|
|
|
|
*
|
|
|
|
* @param html - HTML markup to assign to the page.
|
2022-08-10 21:34:29 +00:00
|
|
|
* @param options - Options to configure how long before timing out and at
|
2020-07-09 13:22:58 +00:00
|
|
|
* what point to consider the content setting successful.
|
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
async setContent(
|
|
|
|
html: string,
|
|
|
|
options: {
|
|
|
|
timeout?: number;
|
|
|
|
waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[];
|
|
|
|
} = {}
|
|
|
|
): Promise<void> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.worlds[PUPPETEER_WORLD].setContent(html, options);
|
2017-11-23 02:44:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* @returns The frame's `name` attribute as specified in the tag.
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* @remarks
|
2020-07-09 13:22:58 +00:00
|
|
|
* If the name is empty, it returns the `id` attribute instead.
|
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* @remarks
|
|
|
|
* This value is calculated once when the frame is created, and will not
|
2020-07-09 13:22:58 +00:00
|
|
|
* update if the attribute is changed later.
|
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
name(): string {
|
2017-06-21 20:51:06 +00:00
|
|
|
return this._name || '';
|
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* @returns The frame's URL.
|
2020-07-09 13:22:58 +00:00
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
url(): string {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#url;
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* @returns The parent frame, if any. Detached and main frames return `null`.
|
2020-07-09 13:22:58 +00:00
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
parentFrame(): Frame | null {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#parentFrame;
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* @returns An array of child frames.
|
2020-07-09 13:22:58 +00:00
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
childFrames(): Frame[] {
|
2017-06-21 20:51:06 +00:00
|
|
|
return Array.from(this._childFrames);
|
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* @returns `true` if the frame has been detached. Otherwise, `false`.
|
2020-07-09 13:22:58 +00:00
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
isDetached(): boolean {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#detached;
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
|
|
|
* Adds a `<script>` tag into the page with the desired url or content.
|
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* @param options - Options for the script.
|
2020-07-09 13:22:58 +00:00
|
|
|
* @returns a promise that resolves to the added tag when the script's
|
|
|
|
* `onload` event fires or when the script content was injected into the
|
|
|
|
* frame.
|
|
|
|
*/
|
|
|
|
async addScriptTag(
|
|
|
|
options: FrameAddScriptTagOptions
|
2022-07-06 07:05:37 +00:00
|
|
|
): Promise<ElementHandle<HTMLScriptElement>> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.worlds[MAIN_WORLD].addScriptTag(options);
|
2017-07-25 18:37:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
|
|
|
* Adds a `<link rel="stylesheet">` tag into the page with the desired url or
|
|
|
|
* a `<style type="text/css">` tag with the content.
|
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* @param options - Options for the style link.
|
2020-07-09 13:22:58 +00:00
|
|
|
* @returns a promise that resolves to the added tag when the stylesheets's
|
|
|
|
* `onload` event fires or when the CSS content was injected into the
|
|
|
|
* frame.
|
|
|
|
*/
|
2022-07-06 07:05:37 +00:00
|
|
|
async addStyleTag(
|
|
|
|
options: FrameAddStyleTagOptions
|
2022-08-10 21:34:29 +00:00
|
|
|
): Promise<ElementHandle<HTMLStyleElement | HTMLLinkElement>> {
|
|
|
|
return this.worlds[MAIN_WORLD].addStyleTag(options);
|
2017-10-04 20:42:26 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* Clicks the first element found that matches `selector`.
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
|
|
|
* @remarks
|
2022-08-10 21:34:29 +00:00
|
|
|
* 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:
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* ```ts
|
2020-07-09 13:22:58 +00:00
|
|
|
* const [response] = await Promise.all([
|
|
|
|
* page.waitForNavigation(waitOptions),
|
|
|
|
* frame.click(selector, clickOptions),
|
|
|
|
* ]);
|
|
|
|
* ```
|
2022-08-10 21:34:29 +00:00
|
|
|
*
|
|
|
|
* @param selector - The selector to query for.
|
2020-07-09 13:22:58 +00:00
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
async click(
|
|
|
|
selector: string,
|
2020-06-23 05:18:46 +00:00
|
|
|
options: {
|
|
|
|
delay?: number;
|
2020-07-02 11:15:39 +00:00
|
|
|
button?: MouseButton;
|
2020-06-23 05:18:46 +00:00
|
|
|
clickCount?: number;
|
|
|
|
} = {}
|
2020-05-07 10:54:55 +00:00
|
|
|
): Promise<void> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.worlds[PUPPETEER_WORLD].click(selector, options);
|
2018-02-05 22:58:03 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* Focuses the first element that matches the `selector`.
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* @param selector - The selector to query for.
|
|
|
|
* @throws Throws if there's no element matching `selector`.
|
2020-07-09 13:22:58 +00:00
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
async focus(selector: string): Promise<void> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.worlds[PUPPETEER_WORLD].focus(selector);
|
2018-02-05 22:58:03 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* Hovers the pointer over the center of the first element that matches the
|
|
|
|
* `selector`.
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* @param selector - The selector to query for.
|
|
|
|
* @throws Throws if there's no element matching `selector`.
|
2020-07-09 13:22:58 +00:00
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
async hover(selector: string): Promise<void> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.worlds[PUPPETEER_WORLD].hover(selector);
|
2018-02-05 22:58:03 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* Selects a set of value on the first `<select>` element that matches the
|
|
|
|
* `selector`.
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
|
|
|
* @example
|
2022-08-12 12:15:26 +00:00
|
|
|
*
|
2022-07-01 11:52:39 +00:00
|
|
|
* ```ts
|
2020-07-09 13:22:58 +00:00
|
|
|
* frame.select('select#colors', 'blue'); // single selection
|
|
|
|
* frame.select('select#colors', 'red', 'green', 'blue'); // multiple selections
|
|
|
|
* ```
|
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* @param selector - The selector to query for.
|
|
|
|
* @param values - The array of values to select. If the `<select>` has the
|
2020-07-09 13:22:58 +00:00
|
|
|
* `multiple` attribute, all values are considered, otherwise only the first
|
|
|
|
* one is taken into account.
|
|
|
|
* @returns the list of values that were successfully selected.
|
2022-08-10 21:34:29 +00:00
|
|
|
* @throws Throws if there's no `<select>` matching `selector`.
|
2020-07-09 13:22:58 +00:00
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
select(selector: string, ...values: string[]): Promise<string[]> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.worlds[PUPPETEER_WORLD].select(selector, ...values);
|
2017-11-02 05:06:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
2022-08-10 21:34:29 +00:00
|
|
|
* Taps the first element that matches the `selector`.
|
2020-07-09 13:22:58 +00:00
|
|
|
*
|
2022-08-10 21:34:29 +00:00
|
|
|
* @param selector - The selector to query for.
|
|
|
|
* @throws Throws if there's no element matching `selector`.
|
2020-07-09 13:22:58 +00:00
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
async tap(selector: string): Promise<void> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.worlds[PUPPETEER_WORLD].tap(selector);
|
2018-02-05 22:58:03 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +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
|
2022-08-12 12:15:26 +00:00
|
|
|
*
|
2022-07-01 11:52:39 +00:00
|
|
|
* ```ts
|
2020-07-09 13:22:58 +00:00
|
|
|
* 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`.
|
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
async type(
|
|
|
|
selector: string,
|
|
|
|
text: string,
|
2022-06-22 13:25:44 +00:00
|
|
|
options?: {delay: number}
|
2020-05-07 10:54:55 +00:00
|
|
|
): Promise<void> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.worlds[PUPPETEER_WORLD].type(selector, text, options);
|
2018-02-05 22:58:03 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 08:37:49 +00:00
|
|
|
/**
|
2022-08-16 06:24:25 +00:00
|
|
|
* @deprecated Use `new Promise(r => setTimeout(r, milliseconds));`.
|
|
|
|
*
|
2020-07-28 08:37:49 +00:00
|
|
|
* 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:
|
|
|
|
*
|
2022-08-16 06:24:25 +00:00
|
|
|
* ```ts
|
2020-07-28 08:37:49 +00:00
|
|
|
* await frame.waitForTimeout(1000);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param milliseconds - the number of milliseconds to wait.
|
|
|
|
*/
|
|
|
|
waitForTimeout(milliseconds: number): Promise<void> {
|
2022-06-22 13:25:44 +00:00
|
|
|
return new Promise(resolve => {
|
2020-07-28 08:37:49 +00:00
|
|
|
setTimeout(resolve, milliseconds);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
|
|
|
* @returns the frame's title.
|
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
async title(): Promise<string> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.worlds[PUPPETEER_WORLD].title();
|
2017-07-27 22:17:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
_navigated(framePayload: Protocol.Page.Frame): void {
|
2017-06-21 20:51:06 +00:00
|
|
|
this._name = framePayload.name;
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#url = `${framePayload.url}${framePayload.urlFragment || ''}`;
|
2017-11-10 23:33:14 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
_navigatedWithinDocument(url: string): void {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#url = url;
|
2018-04-10 06:38:20 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
_onLifecycleEvent(loaderId: string, name: string): void {
|
2017-11-10 23:33:14 +00:00
|
|
|
if (name === 'init') {
|
|
|
|
this._loaderId = loaderId;
|
|
|
|
this._lifecycleEvents.clear();
|
|
|
|
}
|
|
|
|
this._lifecycleEvents.add(name);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
_onLoadingStopped(): void {
|
2018-04-10 22:59:41 +00:00
|
|
|
this._lifecycleEvents.add('DOMContentLoaded');
|
|
|
|
this._lifecycleEvents.add('load');
|
|
|
|
}
|
|
|
|
|
2022-05-17 12:15:44 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_onLoadingStarted(): void {
|
|
|
|
this._hasStartedLoading = true;
|
|
|
|
}
|
|
|
|
|
2020-07-09 13:22:58 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
_detach(): void {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#detached = true;
|
2022-08-10 21:34:29 +00:00
|
|
|
this.worlds[MAIN_WORLD]._detach();
|
|
|
|
this.worlds[PUPPETEER_WORLD]._detach();
|
2022-06-14 11:55:35 +00:00
|
|
|
if (this.#parentFrame) {
|
|
|
|
this.#parentFrame._childFrames.delete(this);
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#parentFrame = null;
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-17 21:27:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
function assertNoLegacyNavigationOptions(options: {
|
|
|
|
[optionName: string]: unknown;
|
|
|
|
}): void {
|
|
|
|
assert(
|
|
|
|
options['networkIdleTimeout'] === undefined,
|
|
|
|
'ERROR: networkIdleTimeout option is no longer supported.'
|
|
|
|
);
|
|
|
|
assert(
|
|
|
|
options['networkIdleInflight'] === undefined,
|
|
|
|
'ERROR: networkIdleInflight option is no longer supported.'
|
|
|
|
);
|
|
|
|
assert(
|
2022-05-31 14:34:16 +00:00
|
|
|
options['waitUntil'] !== 'networkidle',
|
2020-05-07 10:54:55 +00:00
|
|
|
'ERROR: "networkidle" option is no longer supported. Use "networkidle2" instead'
|
|
|
|
);
|
2018-11-20 23:32:46 +00:00
|
|
|
}
|