How to change headless from false to true ? [puppeteer] - puppeteer

How to change headless from false to true ?
How to hide browser?
const browser = await puppeteer.launch({headless: false})
const page = await browser.newPage();
await page.goto(LOGIN_URL, { "waitUntil": "networkidle2" });
await page.evaluate((a) => {
$('input[name="username"]').val(a.username)
$('input[name="password"]').val(a.password)
}, {username, password})
// I want to hide the browser
// do something
await browser.close();

It is not possible to hide the browser in puppeteer at runtime — that's because Chromium can be launched either headless or non-headless only.
But during one script you can first run non-headless browser, close it and then open headless:
let browser = await puppeteer.launch({headless : false});
// 1. Enter requisites, log in to a site
// 2. Save cookies
await browser.close();
browser = await puppeteer.launch({headless : true});
// 3. Load cookies
// 4. Go and do headless stuff

Use this:
const browser = await puppeteer.launch({headless: true});

Related

get tabId from Puppeteer when new tab is created

I'm testing a Chrome extensions and in order to setup everything correctly I would like to know the tabId when a new tab is created
const browser = await puppeteer.launch({ ... });
const appPage = await browser.newPage();
await appPage.goto(appUrl, { waitUntil: 'load' });
// TAB ID??????
I checked the documentation of the Page object but there is nothing about a tabId. So my question is, is it possible to extract a tabId with Puppereer?

puppeteer splitwise login not wokring

I have tried multiple examples to get the splitwise login but unable to get it working.
Although, I'm quite new to puppeteer but login felt a simple usecase for understanding puppeteer.
const puppeteer = require('puppeteer')
const screenshot = 'login.png';
(async () => {
const browser = await puppeteer.launch({headless: false})
const page = await browser.newPage()
await page.goto("https://www.splitwise.com/login", {
waitUntil: 'networkidle2'
});
await page.type('#user_session_email', 'atest')
await page.type('#user_session_password', 'test')
await page.click('[name="commit"]')
await page.waitForNavigation()
browser.close()
console.log('See screenshot: ' + screenshot)
})()
Unfortunately, the page has two forms with identical ids (but different classes) and these forms have inputs with identical ids as well. You just need more specific selectors:
await page.type('form.form-stacked #user_session_email', 'atest')
await page.type('form.form-stacked #user_session_password', 'test')
await page.click('form.form-stacked [name="commit"]')
This does not seem to be a puppeteer issue.
It seems that javascript code in page is actively blocking triggered events somehow.
Are you able to set these values using regular javascript in the console?

Puppeteer not going to next page of multipage form

I have a multipage form and am using puppeteer to test. Problem I have is that when I ask to click to the next button it does click through but it doesn't go to the next page of the form. I have tried waitforNavigation but it throws a timeout issue. I have tried to address the timeout issue but to no success.
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({ width: 1920, height: 1080 });
await page.goto('https://example.com/form/');
//await page.waitForNavigation({timeout: 10000, waitUntil: "load"}); // The promise resolves after navigation has finished
await page.click('#next_button_1'); // Clicking the link will indirectly cause a navigation
await page.setDefaultNavigationTimeout(0);
await page.waitForNavigation();
await page.screenshot({path: 'example.png',fullPage: true});
// other actions...
await browser.close();
})();

In puppeteer how to wait for pop up page to finish loading?

In the following example how do I wait for the pop up window to finish loading?
After clikcing the google icon you get a pop up window to login to gmail, when I try to interact
with the second page it is undefined (as I don't know how to wait for it to fully load.
Any advice?
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({headless: false});
page = await browser.newPage();
await page.goto("https://www.example.com/signin");
await page.waitForSelector(".Icon-google");
await page.click(".Icon-google");
const pages = await browser.pages();
console.log(pages[2].url());
})();
You can wait for a new target to be created.
const browser = await puppeteer.launch({headless: false});
page = await browser.newPage();
await page.goto("https://app.testim.io/#/signin");
await page.waitForSelector(".Icon-google");
const nav = new Promise(res => browser.on('targetcreated', res))
await page.click(".Icon-google");
await nav
const pages = await browser.pages();
console.log(pages.length);//number of pages increases !
console.log(pages.map(page => page.url()));
P.S. first I tried page.waitForNavigation() but it didn't work, probably because it's a popup.
const [newPage] = await Promise.all([
new Promise((resolve) => page.once('popup', resolve)),
page.click('something.that-will-open-the-popup')
]);
await newPage.waitForSelector('.page-is-loaded')

Puppeteer: how to wait all screenshot end before browser close?

Well, I want to screenshot every 20ms during the puppeteer open a page until browser close.However, for the screenshot speed is so fast and it's a long time to generate a pic. So if browser was close, there will be many pics can't be stored.How can I wait all screentshot done before browser close?
You can try to store the data in an array in the higher scope, so that even if the browser closes you can still do some operations in your app.
Have you tried using async/await with your page and browser calls? Try this:
(async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(inputImgUrl, {waitUntil: 'networkidle'});
await page.setViewport(viewPortData);
await page.screenshot({path: outputFile, type: 'jpeg', quality: 50, clip: cropData});
await browser.close();
})();