Puppeteer

How to switch between tabs with Puppeteer?

依然范特西╮ 提交于 2019-12-01 02:01:07
问题 Here is my use case: I have a link which on clicking opens a new tab and loads the content. What I am looking for: Is there a way to switch the reference of the page while new tab opens or create a reference for the new tab? 回答1: Use next function: let clickAndWaitForTarget = async (clickSelector, page, browser) => { const pageTarget = page.target(); //save this to know that this was the opener await page.click(clickSelector); //click on a link const newTarget = await browser.waitForTarget

Can the browser turned headless mid-execution when it was started normally, or vice-versa?

*爱你&永不变心* 提交于 2019-12-01 01:38:28
I want to start a chromium browser instant headless, do some automated operations, and then turn it visible before doing the rest of the stuff. Is this possible to do using Puppeteer, and if it is, can you tell me how? And if it is not, is there any other framework or library for browser automation that can do this? So far I've tried the following but it didn't work. const browser = await puppeteer.launch({'headless': false}); browser.headless = true; const page = await browser.newPage(); await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle2'}); await page.pdf({path: 'hn

Bypassing CAPTCHAs with Headless Chrome using puppeteer

元气小坏坏 提交于 2019-12-01 01:28:51
google finds my browser is being manipulated/controlled/automated by software, and because of that I get captchas . I start chromium manually and do the same steps the captchas doesn't appear. Question 1) Is it possible to solve captcha Programmatically or get rid of it when using puppeteer ? Any way to solve this? Question 2) Does this happens only when without headless option i.e const browser = await puppeteer.launch({ headless: false }) OR this is something the fact we have to accept and move on? Try generating random useragent using this npm package . This usually solves the user agent

Is it possible to pass a React Component to puppeteer?

陌路散爱 提交于 2019-12-01 01:00:40
I have a React component with some componentDidMount logic: export default class MyComponent { componentDidMount() { // some changes to DOM done here by a library } render() { return ( <div>{props.data}</div> ); } } Is it possible to pass this component with props so that everything in componentDidMount() gets executed, somehow to puppeteer in order to take a screenshot? Something along these lines: const browser = await puppeteer.launch({ headless: true }); const page = await browser.newPage(); const html = ReactDOMServer.renderToString(<MyComponent data='' />); <-- but this skips the

variable not assigning inside promise

时光总嘲笑我的痴心妄想 提交于 2019-11-30 21:20:50
Maybe I'm sleepy, but under what circumstances would the following occur? let foo; page .evaluate(() => { // return works... but not closure assignment // doesn't work foo = 'foo'; // works return 'bar'; }) .then(bar => { console.log('foobar', foo, bar); // > foobar undefined bar }); This is occurring in a mocha test using puppeteer update: the exact entire code node 9.11.2 /* global describe, it, before, after */ const fs = require('fs-extra'); const path = require('path'); const assert = require('assert'); const puppeteer = require('puppeteer'); const sleep = require('shleep'); const extPath

How to click on a link that has a certain content in puppeteer?

[亡魂溺海] 提交于 2019-11-30 20:37:47
If I have some content in my page such as: <a>Hi!</a> How can I use Google's Puppeteer to automate the clicking of that element? I need to be able to select it based on its contents alone, not id, class or attribute. Is there something like $('a:contains("Hi!")') that I can leverage to select this element? How can I do this with https://github.com/GoogleChrome/puppeteer thanks Md. Abu Taher First, we have to find element by text. /** * findElemByText - Find an Element By Text * * @param {String} str case-insensitive string to search * @param {String} selector = '*' selector to search * @param

Can the browser turned headless mid-execution when it was started normally, or vice-versa?

无人久伴 提交于 2019-11-30 20:25:58
问题 I want to start a chromium browser instant headless, do some automated operations, and then turn it visible before doing the rest of the stuff. Is this possible to do using Puppeteer, and if it is, can you tell me how? And if it is not, is there any other framework or library for browser automation that can do this? So far I've tried the following but it didn't work. const browser = await puppeteer.launch({'headless': false}); browser.headless = true; const page = await browser.newPage();

How to specify browser language in Puppeteer

回眸只為那壹抹淺笑 提交于 2019-11-30 18:03:40
I would like to launch a Google Chrome browser with language Spanish es using Puppeteer. I've tried puppeteer.launch(args:['--lang=es',...],...) but it didn't work. I've tried passing the environment variable LANGUAGE=es mocha puppeteer-test.js but it didn't work. I've tried using the userDataDir option and passing a folder with a Preferences file a { "intl": { "accept_languages": "es" } } but the browser Settings - Languages still don't show Spanish and neither does window.navigator.languages neither window.navigator.language I'm using Puppeteer 0.11.0 Node 8.4.0 NPM 5.2.0 macOS El Capitan 10

variable not assigning inside promise

微笑、不失礼 提交于 2019-11-30 17:36:39
问题 Maybe I'm sleepy, but under what circumstances would the following occur? let foo; page .evaluate(() => { // return works... but not closure assignment // doesn't work foo = 'foo'; // works return 'bar'; }) .then(bar => { console.log('foobar', foo, bar); // > foobar undefined bar }); This is occurring in a mocha test using puppeteer update: the exact entire code node 9.11.2 /* global describe, it, before, after */ const fs = require('fs-extra'); const path = require('path'); const assert =

How to click on a link that has a certain content in puppeteer?

不问归期 提交于 2019-11-30 16:59:10
问题 If I have some content in my page such as: <a>Hi!</a> How can I use Google's Puppeteer to automate the clicking of that element? I need to be able to select it based on its contents alone, not id, class or attribute. Is there something like $('a:contains("Hi!")') that I can leverage to select this element? How can I do this with https://github.com/GoogleChrome/puppeteer thanks 回答1: First, we have to find element by text. /** * findElemByText - Find an Element By Text * * @param {String} str