Best way to intercept XHR request on page with Puppeteer and return mock response

It seems that the way to go is request.respond() indeed, but still, I couldn’t find a concrete example in the web on how to use it. The way I did it was like this: // Intercept API response and pass mock data for Puppeteer await page.setRequestInterception(true); page.on(‘request’, request => { if (request.url() === constants.API) { … Read more

page.evaluate Vs. Puppeteer $ methods

The main difference between those lines of code is the interaction between the Node.js and the browser environment. The first code snippet will do the following: Run document.querySelector in the browser and return the element handle (to the Node.js environment) Run getProperty on the handle and return the result (to the Node.js environment) Click an … Read more

Puppeteer is not able to install: “ERROR: Failed to set up Chromium r782078! Set “PUPPETEER_SKIP_DOWNLOAD” env variable to skip download”

Note: This is for UbuntuĀ 18.04 (Bionic Beaver). However, it should be a similar process for Windows though. Set the environment variable (as the error suggests): Mac export PUPPETEER_SKIP_DOWNLOAD=’true’ Windows SET PUPPETEER_SKIP_DOWNLOAD=’true’ and then… npm i puppeteer

How can I download images on a page using puppeteer?

If you want to skip the manual dom traversal you can write the images to disk directly from the page response. Example: const puppeteer = require(‘puppeteer’); const fs = require(‘fs’); const path = require(‘path’); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); page.on(‘response’, async response => { const url … Read more

Puppeteer page.evaluate querySelectorAll return empty objects

The values returned from evaluate function should be json serializeable. https://github.com/GoogleChrome/puppeteer/issues/303#issuecomment-322919968 the solution is to extract the href values from the elements and return it. await this.page.evaluate((sel) => { let elements = Array.from(document.querySelectorAll(sel)); let links = elements.map(element => { return element.href }) return links; }, sel);

Rendering WebGL image in headless chrome without a GPU

There’s an open bug which affects systems without X11 libraries: crbug.com/swiftshader/79. It prevents Chrome OS from running with SwiftShader, but the same issue would also happen on a headless Linux system which has no X11 support. Fortunately it should be feasible to install X11 and get things running. I’m not 100% sure which packages provide … Read more

How to use proxy in puppeteer and headless Chrome?

You can find an example about proxy at here ‘use strict’; const puppeteer = require(‘puppeteer’); (async() => { const browser = await puppeteer.launch({ // Launch chromium using a proxy server on port 9876. // More on proxying: // https://www.chromium.org/developers/design-documents/network-settings args: [ ‘–proxy-server=127.0.0.1:9876’ ] }); const page = await browser.newPage(); await page.goto(‘https://google.com’); await browser.close(); })();

Set localstorage items before page loads in puppeteer?

You have to register localStorage item like this: await page.evaluate(() => { localStorage.setItem(‘token’, ‘example-token’); }); You should do it after page page.goto – browser must have an url to register local storage item on it. After this, enter the same page once again, this time token should be here before the page is loaded. Here … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)