10 examples of 'puppeteer wait for page to load' in JavaScript

Every line of 'puppeteer wait for page to load' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
139async wait(pageCompleteCheck) {
140 const waitTime = this.options.pageCompleteWaitTime || 5000;
141 if (!pageCompleteCheck) {
142 pageCompleteCheck = this.options.pageCompleteCheckInactivity
143 ? pageCompleteCheckByInactivity
144 : defaultPageCompleteCheck;
145 // if using SPA just override
146 if (this.options.spa) {
147 pageCompleteCheck = spaCheck;
148 }
149 }
150
151 const driver = this.driver,
152 pageCompleteCheckTimeout = this.options.timeouts.pageCompleteCheck;
153 try {
154 const pageCompleteCheckCondition = new Condition(
155 'for page complete check script to return true',
156 function(d) {
157 return d.executeScript(pageCompleteCheck, waitTime).then(function(t) {
158 return t === true;
159 });
160 }
161 );
162 log.debug(
163 `Waiting for script pageCompleteCheck at most ${pageCompleteCheckTimeout} ms`
164 );
165 log.verbose(`Waiting for script ${pageCompleteCheck}`);
166 await timeout(
167 driver.wait(
168 pageCompleteCheckCondition,
169 pageCompleteCheckTimeout,
170 undefined,
171 this.options.pageCompleteCheckPollTimeout || 200
172 ),
173 pageCompleteCheckTimeout,
174 `Running page complete check ${pageCompleteCheck} took too long `
175 );
176 log.debug(`Waiting after load event for ${waitTime} ms.`);
177 await delay(waitTime);
178 } catch (e) {
179 log.error('Failed to wait ' + e);
180 throw new UrlLoadError('Failed to wait ', {
181 cause: e
182 });
183 }
184}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

108public async waitFor(selector: string | EvaluateFn, options?: WaitForSelectorOptions, ...args: Array<SerializableOrJSHandle>): Promise<void> {
109 await this.page.waitFor(selector, options, ...args);
110}
54public async waitForSelector(selector: string) {
55 return this.page.waitForSelector(selector)
56}
64async function loadPage (driver, url) {
65 await retryInterval(5, 1000, async function () {
66 await openPage(driver, url);
67 const title = await spinTitle(driver);
68 title.should.not.include('Cannot Open Page');
69 });
70}
79waitForDocumentFullyLoaded() {
80 driver.waitUntil(
81 () => {
82 const webview = driver.execute(() => ({ state: document.readyState, title: document.title }));
83
84 return webview.state === DOCUMENT_READY_STATE.COMPLETE && webview.title !== '';
85 },
86 15000,
87 'Website not loaded',
88 100,
89 );
90}
15wait(elem) {
16 browser.waitForExist(elem);
17}
22public async click(element: ElementHandle): Promise<void> {
23 await element.click();
24}
481public async waitURL(expectedUrl: string, timeout: number = TestConstants.TS_SELENIUM_DEFAULT_TIMEOUT) {
482 Logger.trace(`DriverHelper.waitURL ${expectedUrl}`);
483
484 await this.getDriver().wait(async () => {
485 const currentUrl: string = await this.getDriver().getCurrentUrl();
486 const urlEquals: boolean = currentUrl === expectedUrl;
487
488 if (urlEquals) {
489 return true;
490 }
491 });
492}
170waitForNavigation(timeout?: number) {
171 this.push(
172 async () => await this.page.waitForNavigation({ timeout }),
173 prepareStackTrace()
174 )
175
176 return this
177}
126public async getPage(
127 index: number = 0,
128 waitForRequests: boolean = false,
129 timeoutMs: number = 5000
130): Promise<DecoratedPage> {
131 /**
132 * Wait for the page at index to be ready and activate it.
133 */
134 logger.verbose(`Browser: getPage(${index})`);
135
136 const page = await waitFor(() => {
137 if (index >= this._pages.length) return null;
138 return this._pages[index];
139 }, timeoutMs);
140
141 if (!page) {
142 throw new Error(`Browser: getPage(${index}) timed out`);
143 }
144
145 // when headless = false the tab needs to be activated
146 // for the execution context to run
147 await page.super.bringToFront();
148
149 if (waitForRequests) {
150 await page.waitForRequests();
151 }
152
153 this._currentPageIndex = index;
154 logger.verbose(`Browser: getPage(${index}) activated ${page.super.url()}`);
155
156 return page.super;
157}

Related snippets