Puppeteer eval issues - javascript

I am having issues trying to get this to work.
I am needing to select the calendar heading "May 2020", but am not having any luck, could I get someone to look at what I am doing wrong?
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://www.stayz.com.au/holiday-rental/p9177051?noDates=true',{waitUntil: 'domcontentloaded'});
const headingTxt = await page.evaluate(() =>
document.querySelector('#rates-availability > div > div > section > div > div.inline-calendar > div > div.cal-controls__calendar-parent--middle-multi > div > div:nth-child(2) > h4 > span').innerText);
console.log('');
console.log('========[ output ]======== ', headingTxt);
console.log('');
await page.close();
await browser.close();
})();

This selector needs scrolling and wide viewport to appear:
'use strict';
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({ headless: false, args: ['--start-maximized'] }); //
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 850 });
await page.goto('https://www.stayz.com.au/holiday-rental/p9177051?noDates=true', {waitUntil: 'domcontentloaded'});
const selector = '#rates-availability > div > div > section > div > div.inline-calendar > div > div.cal-controls__calendar-parent--middle-multi > div > div:nth-child(2) > h4 > span';
while ((await page.$(selector)) === null) {
await page.evaluate(() => { window.scrollBy(0, window.innerHeight); });
await page.waitFor(1000);
}
const headingTxt = await page.evaluate(
selector => document.querySelector(selector).innerText,
selector
);
console.log('');
console.log('========[ output ]======== ', headingTxt);
console.log('');
await page.close();
await browser.close();
})();

Related

How to solve nodejs puppeteer Cannot find module 'puppeteer' error

I have installed puppeteer
`
const automation = require("puppeteer");
const fs = require("fs");
const automationConfiguration = {
headless: false
}
async function runautomation() {
const browser = await automation.launch(automationConfiguration)
const page = await browser.newPage()
const navigationPromise = page.waitForNavigation()
await navigationPromise
await page.goto('https://www.google.com/search?q=perfmatters+vs+wp+rocket&oq=perf&aqs=chrome.0.69i59j69i57j0i67j46i433i512j0i433i512l2j0i131i433i512j69i64.961j0j15&sourceid=chrome&ie=UTF-8')
await page.setViewport({ width: 1366, height: 657 })
await page.waitForSelector('.MjjYud:nth-child(5) > .g > .kvH3mc > .Z26q7c > .yuRUbf > a > .LC20lb')
await page.click('.MjjYud:nth-child(5) > .g > .kvH3mc > .Z26q7c > .yuRUbf > a > .LC20lb')
await navigationPromise
await browser.close() }
runautomation()
`
I have tried everyting from my end but nothing works im new to puppeteer please help

Puppeteer can't click on li element

I'm trying to access a telegram conversation at https://web.telegram.org/k/. I can't click on the li element, I've tried several ways like page.click() and document.querySelector("xxxxxx").click() but nothing works.
Here's my code:
const pup = require('puppeteer')
const url = 'https://web.telegram.org/k/'
async function inicializar() {
const browser = await pup.launch({
headless: false
})
const page = await browser.newPage()
await page.goto(url)
await page.waitForTimeout(15000)
await page.evaluate(() => {
document.querySelector("#auth-pages > div > div.tabs-container.auth-pages__container > div.tabs-tab.page-signQR.active > div > div.input-wrapper > button > div").click()
})
await page.waitForTimeout(5000)
try {
await page.evaluate(() => {
document.querySelector("#auth-pages > div > div.tabs-container.auth-pages__container > div.tabs-tab.page-signQR.active > div > div.input-wrapper > button:nth-child(2) > div").click()
})
} catch {
await page.evaluate(() => {
document.querySelector("#auth-pages > div > div.tabs-container.auth-pages__container > div.tabs-tab.page-sign.active > div > div.input-wrapper > button:nth-child(6) > div").click()
})
}
await page.waitForTimeout(5000)
await page.evaluate(() => {
document.querySelector("#auth-pages > div > div.tabs-container.auth-pages__container > div.tabs-tab.page-sign.active > div > div.input-wrapper > div.input-field.input-field-phone > div.input-field-input").childNodes[0].textContent = "+55 11 944407845"
document.querySelector("#auth-pages > div > div.tabs-container.auth-pages__container > div.tabs-tab.page-sign.active > div > div.input-wrapper > button.btn-primary.btn-color-primary.rp > div").click()
})
await page.waitForTimeout(30000)
const foo = await page.$x('//*[#id="column-left"]/div/div/div[1]/div[2]/input');
await foo[0].type("folio_bill_bot")
await page.waitForTimeout(5000)
const xp = `/html/body/div[1]/div[1]/div[1]/div/div/div[2]/div[2]/div[2]/div/div/div[1]/div/div[1]/ul/li/div[1]`;
const sizeButton = await page.waitForXPath(xp);
await sizeButton.evaluate(btn => {
btn.closest("li").dispatchEvent(new Event("mousedown"));
});
await page.waitForTimeout(10000);
//await Promise.all([foo2[0].click()])
//await foo2[0].click()
return [page, browser]
}
inicializar()
For those who want to look at the HTML:

Loop through pages to return all the product links

The goal is to obtain all product links from all pages in the pagination. So far I have managed to print information to the console with console.log (links). However, since I am completely new to this field and completely inexperienced, I have a problem how to pass some value back with the return command. return links.
With console.log(links) I get a warning: getLinks is not iterable
const puppeteer = require('puppeteer')
async function getLinks(){
const browser = await puppeteer.launch({headless: false, defaultViewport: null});
const page = await browser.newPage();
const url = "https://example.com/product-category?p=1&nidx"
await page.goto(url)
while(await page.$('.change-country-buttons > button:nth-child(1)')){
await page.waitForTimeout(2000);
await page.keyboard.press('ArrowDown');
await page.waitForSelector('.change-country-buttons');
await page.waitForTimeout(2000);
await page.click('.change-country-buttons > button:nth-child(1)');
await page.waitForTimeout(2000);
}
while(await page.$(".pagination .pagination--next")){
await page.waitForTimeout(2000);
await page.evaluate(() => {
document.querySelector(".pagination .pagination--next").scrollIntoView();
});
await page.waitForTimeout(1000);
await page.waitForSelector(".pagination .pagination--next")
await page.waitForTimeout(500);
await page.click('.pagination .pagination--next')
const links = await page.$$eval('.item__info > .mtc-link:nth-child(2)', (allAs) => { return allAs.map((a) => a.href) });
await page.waitForTimeout(1500);
console.log(links)
}
}
return links // Is returning links only form the first page and then the loop stops
I tried something with Promise.all () but it wasn't entirely clear to me how to do it.
Please help and be gentle as I am just starting to learn the basics
You need to create an array and push all the helmet links from each page onto it.
This tested successfully for me.
const puppeteer = require('puppeteer')
async function getLinks(){
const browser = await puppeteer.launch({headless: false, defaultViewport: null});
const page = await browser.newPage();
const url = "https://www.motocard.com/en/motorcycle-road-gear/helmets/precio_150-3200/full-face?p=1&nidx"
var all_links = [];
await page.goto(url);
while(await page.$('.change-country-buttons > button:nth-child(1)')){
await page.waitForTimeout(2000);
await page.keyboard.press('ArrowDown');
await page.waitForSelector('.change-country-buttons');
await page.waitForTimeout(2000);
await page.click('.change-country-buttons > button:nth-child(1)');
await page.waitForTimeout(2000);
}
while(await page.$(".pagination .pagination--next")){
await page.waitForTimeout(2000);
await page.evaluate(() => {
document.querySelector(".pagination .pagination--next").scrollIntoView();
});
await page.waitForTimeout(1000);
await page.waitForSelector(".pagination .pagination--next")
await page.waitForTimeout(500);
await page.click('.pagination .pagination--next')
const links = await page.$$eval('.item__info > .mtc-link:nth-child(2)', (allAs) => { return allAs.map((a) => a.href) });
await page.waitForTimeout(1500);
//console.log(links)
all_links.push(...links);
}
return all_links;
}
(async ()=>{
var links = await getLinks();
console.log('done');
console.log(links);
})();

How to fill out form in puppeteer

I'm having trouble signing in, when I open this page
https://www.nike.com.br/chuteira-nike-phantom-gt-elite-3d-unissex-153-169-171-316414?gridPosition=A1
It goes to another page, and I can't fill out the form, can someone help me fill out the login form?
(async () => {
try{
console.log("Started!")
const browser = await puppeteer.launch({
executablePathh:'/usr/bin/chromium', headless:false,
});
const page = await browser.newPage();
await page.setViewport({ width: 1920, height: 1080 });
await page.setRequestInterception(true);
const blockedResourceTypes = ["image", "bacon", "imageset", "font", "stylesheet", "texttrack", "csp_report"]
const blockedURLs = [
""
]
const allowedRequest = req => !blockedResourceTypes.includes(req.resourceType()) && !blockedURLs.includes(req.url())
page.on('request', (req) => {
if(allowedRequest(req)) {
req.continue();
}
else {
req.abort();
}
});
await page.goto('https://www.nike.com.br/chuteira-nike-phantom-gt-elite-3d-unissex-153-169-171-316414?gridPosition=A1', {
waitUntil: 'domcontentloaded', timeout:0});
await page.waitForXPath('//label[#for="tamanho__idM40F395"]', {
visible:true, timeout:0});
const tamanho = await page.$x('//label[#for="tamanho__idM40F395"]')
await tamanho[0].click('//label[#for="tamanho__idM40F395"]');
await page.waitForSelector('button#anchor-acessar-unite-oauth2') await page.click('button#anchor-acessar-unite-oauth2')
const iframe = wait.ForSelector('iframe#nike-unite-oauth2-iframe')
const frame = await iframe.contentFrame()
await frame.type('input[name="emailAddress"]', 'test#gmail.com')
await frame.type('input[name="password"]', 'pass')
await frame.click('input[value="ENTRAR"]')
They realized that if you run the script it will go to the login page and then I can't fill out the form
To login in a new page instead of an iframe, try to replace this:
await page.click('button#anchor-acessar-unite-oauth2')
const iframe = wait.ForSelector('iframe#nike-unite-oauth2-iframe')
const frame = await iframe.contentFrame()
await frame.type('input[name="emailAddress"]', 'test#gmail.com')
await frame.type('input[name="password"]', 'pass')
await frame.click('input[value="ENTRAR"]')
with this:
await Promise.all([
page.click('button#anchor-acessar-unite-oauth2'),
page.waitForNavigation(),
])
await page.waitForSelector('input[name="emailAddress"]')
await page.type('input[name="emailAddress"]', 'test#gmail.com')
await page.type('input[name="password"]', 'pass')
await page.click('input[value="ENTRAR"]')

Puppeteer how to read and use data from a text file

Hi I'm working on a script to learn node JS, and I'm stuck at this point :
I would like to read a text file with my email: password, and with that I would like to open as many tabs as many email:password I have and use each email: password to connect to the website
const puppeteer = require("puppeteer");
const lineReader = require("line-reader");
(async () => {
let data = [];
const promises = []
let int = 0;
lineReader.eachLine("c.txt", function(line) {
int++;
data = line.split(":")
console.log(data);
});
console.log(int);
for (let i = 0; i < 2; i++) {
const browser = await puppeteer.launch({
headless: false
});
const page = await browser.newPage();
const navigationPromise = page.waitForNavigation();
await page.goto("https:www.site.com/en/launch/");
await page.setViewport({ width: 1920, height: 1080 });
await page.waitForSelector(
".d-sm-h > .bg-white > .right-nav > .member-nav-item > .join-log-in"
);
await page.click(
".d-sm-h > .bg-white > .right-nav > .member-nav-item > .join-log-in"
);
const emailInput = '[placeholder="Adresse e-mail"][autocomplete="email"]';
await page.waitForSelector(emailInput, { timeout: 0 });
await page.focus(emailInput);
await page.keyboard.type(data[0]);
const passwordInput =
'[placeholder="Mot de passe"][autocomplete="current-password"]';
await page.waitForSelector(passwordInput, { timeout: 0 });
await page.focus(passwordInput);
await page.keyboard.type(data[1]);
await page.click(
".site-unite-submit-button.loginSubmit.site-unite-component"
);
}
await Promise.all(promises)
})();
but what I have when I consol log my data :
[ 'email#gmail.com:tesssst' ]
[ 'email2#gmail.com:tesssst' ]
I would like to know and understand how can I use each email:password that i have on my txt file to open tabs and use my data to login to the website
Thank you

Categories

Resources