Puppeteer File chooser - node.js

I want to upload files on Facebook by this button 'add photos', but actually its not a button its a div... I don't know how to upload files. I've tried filechooser with puppeteer#2.0.0 but it doesn't work. Pls help
File chooser doesn't work :D
(async () => {
const url = 'https://facebook.com';
let selector = '#mount_0_0 > div > div:nth-child(1) > div.rq0escxv.l9j0dhe7.du4w35lb > div.rq0escxv.l9j0dhe7.du4w35lb > div > div > div.j83agx80.cbu4d94t.d6urw2fd.dp1hu0rb.l9j0dhe7.du4w35lb > div.l9j0dhe7.dp1hu0rb.cbu4d94t.j83agx80 > div.j83agx80.cbu4d94t > div > div > div > div > div.rq0escxv.l9j0dhe7.du4w35lb.qmfd67dx.gile2uim.buofh1pr.g5gj957u.hpfvmrgz.aov4n071.oi9244e8.bi6gxh9e.h676nmdw.aghb5jc5 > div:nth-child(1) > div > div > div > div > div > div > div.n00je7tq.arfg74bv.qs9ysxi8.k77z8yql.i09qtzwb.n7fi1qx3.b5wmifdl.hzruof5a.pmk7jnqg.j9ispegn.kr520xx4.c5ndavph.art1omkt.ot9fgl3s.rnr61an3';
const list = [
"https://www.facebook.com/groups/147330058776765","https://www.facebook.com/groups/147330058776732"
]
try {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 800 })
await page.goto('https://www.facebook.com/groups/147330058776765');
await page.click('#email');
await page.keyboard.type('login');
await page.click('#pass');
await page.keyboard.type('password');
await page.click('#u_0_2');
await page.waitForSelector('#mount_0_0 > div > div:nth-child(1) > div.rq0escxv.l9j0dhe7.du4w35lb > div.rq0escxv.l9j0dhe7.du4w35lb > div > div > div.j83agx80.cbu4d94t.d6urw2fd.dp1hu0rb.l9j0dhe7.du4w35lb > div.l9j0dhe7.dp1hu0rb.cbu4d94t.j83agx80 > div.j83agx80.cbu4d94t > div > div > div > div > div.rq0escxv.l9j0dhe7.du4w35lb.qmfd67dx.gile2uim.buofh1pr.g5gj957u.hpfvmrgz.aov4n071.oi9244e8.bi6gxh9e.h676nmdw.aghb5jc5 > div:nth-child(1) > div > div > div > div > div > div').then(() => console.log('got it'));
await page.click('#mount_0_0 > div > div:nth-child(1) > div.rq0escxv.l9j0dhe7.du4w35lb > div.rq0escxv.l9j0dhe7.du4w35lb > div > div > div.j83agx80.cbu4d94t.d6urw2fd.dp1hu0rb.l9j0dhe7.du4w35lb > div.l9j0dhe7.dp1hu0rb.cbu4d94t.j83agx80 > div.j83agx80.cbu4d94t > div > div > div > div > div.rq0escxv.l9j0dhe7.du4w35lb.qmfd67dx.gile2uim.buofh1pr.g5gj957u.hpfvmrgz.aov4n071.oi9244e8.bi6gxh9e.h676nmdw.aghb5jc5 > div:nth-child(1) > div > div > div > div > div > div');
await page.waitForSelector('#mount_0_0 > div > div:nth-child(1) > div.rq0escxv.l9j0dhe7.du4w35lb > div:nth-child(5) > div > div > div.rq0escxv.l9j0dhe7.du4w35lb > div > div.iqfcb0g7.tojvnm2t.a6sixzi8.k5wvi7nf.q3lfd5jv.pk4s997a.bipmatt0.cebpdrjk.qowsmv63.owwhemhu.dp1hu0rb.dhp61c6y.l9j0dhe7.iyyx5f41.a8s20v7p > div > div > div > div > div.kr520xx4.pedkr2u6.ms05siws.pnx7fd3z.b7h9ocf4.pmk7jnqg.j9ispegn.k4urcfbm > div:nth-child(3) > div.n1l5q3vz.hv4rvrfc.dati1w0a > div.aov4n071 > div > div > div > div > div');
const [fileChooser] = await Promise.all([
page.waitForFileChooser(),
page.click('#mount_0_0 > div > div:nth-child(1) > div.rq0escxv.l9j0dhe7.du4w35lb > div:nth-child(5) > div > div > div.rq0escxv.l9j0dhe7.du4w35lb > div > div.iqfcb0g7.tojvnm2t.a6sixzi8.k5wvi7nf.q3lfd5jv.pk4s997a.bipmatt0.cebpdrjk.qowsmv63.owwhemhu.dp1hu0rb.dhp61c6y.l9j0dhe7.iyyx5f41.a8s20v7p > div > div > div > div > div.kr520xx4.pedkr2u6.ms05siws.pnx7fd3z.b7h9ocf4.pmk7jnqg.j9ispegn.k4urcfbm > div:nth-child(3) > div.n1l5q3vz.hv4rvrfc.dati1w0a > div.aov4n071 > div > div > div > div > div'),
])
await fileChooser.accept(['/example.png']);
await browser.close();
} catch (e) {console.log('main program error:' + e);}
})();

Related

Can't get member count and daily post count in a Facebook group using puppeteer

Been trying to get the member count and daily post count of different Facebook groups. Kept getting null or TypeError. Got those query selectors when I copied the selector from chrome.
const puppeteer = require('puppeteer')
const fs = require('fs')
// get the amount of members
const start = async () => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto('https://www.facebook.com/groups/free.code.camp.world')
const memberCount = await page.evaluate(() => {
return document.querySelector("#mount_0_0_Hj > div > div:nth-child(1) > div > div.rq0escxv.l9j0dhe7.du4w35lb > div > div > div.j83agx80.cbu4d94t.d6urw2fd.dp1hu0rb.l9j0dhe7.du4w35lb > div.l9j0dhe7.dp1hu0rb.cbu4d94t.j83agx80 > div:nth-child(1) > div.rq0escxv.l9j0dhe7.du4w35lb.j83agx80.pfnyh3mw.taijpn5t.gs1a9yip.owycx6da.btwxx1t3.ihqw7lf3.cddn0xzi > div > div > div > div > div:nth-child(1) > div > div > div > div > div > div:nth-child(2) > span > div > div:nth-child(3) > a").textContent
})
// get the amount of daily posts
const dailyPost = await page.evaluate(() => {
return document.querySelector("#mount_0_0_Hj > div > div:nth-child(1) > div > div.rq0escxv.l9j0dhe7.du4w35lb" +
" > div > div > div.j83agx80.cbu4d94t.d6urw2fd.dp1hu0rb.l9j0dhe7.du4w35lb >" +
" div.l9j0dhe7.dp1hu0rb.cbu4d94t.j83agx80 > div.j83agx80.cbu4d94t.buofh1pr > div > div:nth-child(2) >" +
" div > div > div > div:nth-child(3) > div > div > div > div > div > div:nth-child(2) > div >" +
" div:nth-child(1) > div > div > div.rq0escxv.l9j0dhe7.du4w35lb.j83agx80.cbu4d94t.d2edcug0.hpfvmrgz.rj1gh0hx.buofh1pr.g5gj957u.o8rfisnq.p8fzw8mz.pcp91wgn.iuny7tx3.ipjc6fyt > div > div:nth-child(1) > span").textContent
})
console.log(memberCount)
console.log(dailyPost)
await browser.close()
}
start()

How to speed up Node js puppeteer typing?

Hello can someone please with puppeteer, The code i have is working but typing long words takes for ever is there a way to speed up typing?
The problem is the typing takes way to long when i have a lot of words in a text file. want a way to just paste from .tx file without typing
const fsExtra = require('fs-extra')
const puppeteer = require('puppeteer-extra')
const StealthPlugin = require('puppeteer-extra-plugin-stealth')
puppeteer.use(StealthPlugin())
// Start building links
const links = fs.readFileSync('robux4.txt').toString().split('\r\n');
const finalLinks = links.join().replace(/,/g, '\n');
let halfLinks1 = links.slice(0, 60000);
puppeteer.launch({ headless: false }).then(async browser => {
const page = await browser.newPage()
// Build
await page.goto('https://www.site.ec.illinois.edu/account/my-account', {
waitUntil: 'networkidle0',
timeout: 110000,
});
await page.waitForSelector('#input_comp-jzxudh31')
let random1 = Math.random().toString(36).substring(10);
let random2 = Math.random().toString(36).substring(10);
let random3 = Math.random(3).toString(36).substring(10);
await page.type('#input_comp-jzxudh31', random1 + random2 ,)
await page.type('#input_comp-jzxudh50', random1 + random2 ,)
await page.type('#input_comp-jzxudh50', '#gmail.com')
await page.type('#input_comp-jzxudh60', random1 + random2)
await page.click('._1fbEI')
await page.waitFor(5000)
await page.goto('https://www.site.ec.illinois.edu/account/my-account', {
waitUntil: 'networkidle0',
timeout: 110000,
});
await page.waitForSelector('.sAa6lj > #profileVisibility > .sfYIV2 > .s3Fb6T > .s1b4VH')
await page.click('.sAa6lj > #profileVisibility > .sfYIV2 > .s3Fb6T > .s1b4VH')
await page.waitForSelector('div > div > .s3xEru > .s1CVDb > .s3UsU7')
await page.click('div > div > .s3xEru > .s1CVDb > .s3UsU7')
await page.waitForSelector('#TPASection_k14nm80e > div > div.sHCADR > div > div > section > div > div > div.s3uVoc > div > div.s1Dlxl > button.s1CVDb.s3X1VX.s2uKgn.o3x-IY--upgrade.s2_1ar.oYbiBO--primary')
await page.click('#TPASection_k14nm80e > div > div.sHCADR > div > div > section > div > div > div.s3uVoc > div > div.s1Dlxl > button.s1CVDb.s3X1VX.s2uKgn.o3x-IY--upgrade.s2_1ar.oYbiBO--primary')
await page.waitForSelector('#display-name-id')
const input = await page.$('#display-name-id');
await input.click({ clickCount: 3 })
await page.waitFor(2000)
await input.type('FREE ROBUX CODES - HOW TO GET FREE ROBUX [' + random3 + ']');
await page.waitFor(2000)
await page.waitForSelector('#TPASection_k14nm80e > div > div.s1bpIa > form > div.s2faAG > div:nth-child(2) > div > div > button.s1CVDb.s3X1VX.s2uKgn.o3x-IY---priority-7-primary.o3x-IY--upgrade.stQWOt')
await page.click('#TPASection_k14nm80e > div > div.s1bpIa > form > div.s2faAG > div:nth-child(2) > div > div > button.s1CVDb.s3X1VX.s2uKgn.o3x-IY---priority-7-primary.o3x-IY--upgrade.stQWOt')
await page.waitForSelector('#comp-k14nm5yw > ul > li:nth-child(1) > div')
await page.click('#comp-k14nm5yw > ul > li:nth-child(1) > div')
await page.waitForNavigation
await page.waitForSelector('#TPASection_kkfzu8kj > div > div.s7lnRg > div.s3hIHm.o1DNob--childrenFullWidth > div > div > div.sGjTGl.o2GAvo--withHoverColor > div.sugWin > div.s2neI5.opo9uN---typography-11-runningText.opo9uN---priority-7-primary.s1uyGa')
await page.waitFor(2000)
await page.click('#TPASection_kkfzu8kj > div > div.s7lnRg > div.s3hIHm.o1DNob--childrenFullWidth > div > div > div.sGjTGl.o2GAvo--withHoverColor > div.sugWin > div.s2neI5.opo9uN---typography-11-runningText.opo9uN---priority-7-primary.s1uyGa')
let link1 = halfLinks1.join().replace(/,/g, '\n')
let content = link1;
await page.waitForSelector('#placeholder-editor')
await page.click('#placeholder-editor')
await page.type('#placeholder-editor', content)
await page.waitForTimeout(14000)
await page.click('#TPASection_kkfzu8kj > div > div.s1CcJ8 > div > div > div > section > div > div > div.s2DXU2.s1_fDS > div.s17Yb3 > button.s2cOuO.s1ZhWc.s2lWPV.opZNt_--upgrade.s22DiS.oyXUyw--primary')
await page.waitForTimeout(3000)
const link = await page.evaluate(() => location.href);
fs.appendFileSync('links2.txt', 'FREE ROBUX CODES' + '\n')
await browser.close();
}) ```

puppeteer text box with no id

I want to use Puppeteer to enter a value in an input field. Ive done it for most of a web page but having a real problem with a specific field that doesn't have an id or a good label.
here is the inspect elements
<div class="retype-security-code"><input type="text" class="form-text mask-cvv-4" aria-label="Security code" placeholder="CVV2" value=""><img src="https://c1.neweggimages.com/WebResource/Themes/Nest/images/cvv2_sm.gif" alt="cvv2"></div>
<input type="text" class="form-text mask-cvv-4" aria-label="Security code" placeholder="CVV2" value="">
image of code above
here is some code that Ive been playing with
while (true) {
try {
await page.waitForSelector('#cvv2Code' , {timeout: 500})
await page.type('#cvv2Code', config.cv2)
break
}
catch (err) {}
try {
await page.waitForSelector('#creditCardCVV2' , {timeout: 500})
await page.type('#creditCardCVV2', config.cv2)
break
}
catch (err) {}
try {
await page.waitForSelector('#app > div > section > div > div > form > div.row-inner > div.row-body > div > div:nth-child(3) > div > div.checkout-step-body > div.checkout-step-done > div.card > div.retype-security-code > input' , {timeout: 500})
await page.focus('#app > div > section > div > div > form > div.row-inner > div.row-body > div > div:nth-child(3) > div > div.checkout-step-body > div.checkout-step-done > div.card > div.retype-security-code > input')
await page.keyboard.type('###')
break
}
catch (err) {}
}
Why are you using #cvv2Code and #creditCardCVV2 as selectors when they are not in your html code, nor in the picture?
This class form-text mask-cvv-4 seems like a reasonable option for the field:
await page.waitForSelector('.form-text.mask-cvv-4');
Those selectors in the last try block are too long, that's unmaintainable and hard to read, avoid writing such selectors.
Also, please add all relevant error messages to your question, it's hard(er) to help you without it.
What I found you can do is find the element using querySelector and then use setAttribute to give the elemtent an id.
So something like
await page.evaluate(() => {
const inputBox = document.querySelector(".form-text mask-cvv-4");
inputBoxes.setAttribute('id', 'inputBox1');
});
await page.type("#inputBox1", "yourText");
If the element has children then you'd just have to get the children from the element. If there is multiple elements with the class tag you can use querySelectorAll and loop through that list.

timeout error with navigation and waitForSelector() in puppeteer irrespective of timeout value

I want my program to do this:
open a web page
click on a button to go to a new page
take a screenshot of the new page.
Steps 1 and 2 are working fine but I'm running into timeout error with step 3. Based on responses to similar questions on StackOverflow, I used waitForNavigation() with bigger timeout spans (up to 2 min) but I'm still getting the same error. Using waitForSelector() instead of waitForNavigation() is also giving the same error. If I remove both, puppeteer takes a screenshot of the webpage in step 1. I have also tried using different options with waitUntil, such as "domcontentloaded", "loaded", "networkidle0" and "newtorkidle2", but nothing is working.
This is my first program in puppeteer and I've been stuck on this problem for a long time.
Here's my code:
await page.waitForSelector('#featured > c-wiz > div.OXo54d > div > div > div > span > span > span.veMtCf');
// await navigation;
await page.screenshot({path: 'learnmore.png'});
console.log('GOT THIS FAR:)');
//await page.close();
await browser.close();
return 0;
Here's the complete program:
const puppeteer = require('puppeteer');
(async () => {
try{
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
// const navigationPromise = page.waitForNavigation({waitUntil: "load"});
//google.com
await page.goto('https://google.com');
await page.type('input.gLFyf.gsfi',"hotels in london");
await page.keyboard.press('Enter');
//search results
// await navigationPromise;
await page.waitForSelector('#rso > div:nth-child(2) > div > div > div > g-more-link > a > div');
await page.click('#rso > div:nth-child(2) > div > div > div > g-more-link > a > div');
//list of hotels
// await navigationPromise;
await page.waitForSelector('#yDmH0d > c-wiz.zQTmif.SSPGKf > div > div.lteUWc > div > c-wiz > div > div.gpcwnc > div.cGQUT > main > div > div.Hkwcrd.Sy8xcb.XBQ4u > c-wiz > div.J6e2Vc > div > div > span > span');
await page.click("#yDmH0d > c-wiz.zQTmif.SSPGKf > div > div.lteUWc > div > c-wiz > div > div.gpcwnc > div.cGQUT > main > div > div.Hkwcrd.Sy8xcb.XBQ4u > c-wiz > div.l5cSPd > c-wiz:nth-child(3) > div > div > div > div.kCsInf.ZJqrAd.qiy8jf.G9g6o > div > div.TPQEac.qg10C.RCpQOe > a > button > span");
//"learn more"
// await navigationPromise;
//This is where timeout error occurs:
await page.waitForSelector('#featured > c-wiz > div.OXo54d > div > div > div > span > span > span.veMtCf');
// await navigation;
await page.screenshot({path: 'learnmore.png'});
console.log('GOT THIS FAR:)');
//await page.close();
await browser.close();
return 0;
}
catch(err){
console.error(err);
}
})()
.then(resolvedValue => {
console.log(resolvedValue);
})
.catch(rejectedValue => {
console.log(rejectedValue);
})
Your timeout occurs because the selector you are waitng for is not exist on the page. (If you are opening the browser console where the script stucks and launch $(selector) it will return null)
Google uses dynamic class and id values, exactly to prevent (or to make it harder) to retrieve data by scripts, the selectors will have different values everytime you visit the page.
If you really need to scrape its content you can use XPath selectors which are less fragile compared to dynamically changing selector names:
E.g.:
await page.waitForXpath('//h3[contains(text(), "The Best Hotels in London")]')
const link = await page.$x('//h3[contains(text(), "The Best Hotels in London")]')
await link[0].click()
Docs references:
page.waitForXpath
page.$x

Using node.js / puppeteer to print form elements?

I'm accessing the web interface of a Ricoh printer. I cannot identify the user name and password fields. Normally I "inspect" the form element and copy the "CSS Selector". This method works for other forms but not this one. Here is the "CSS Selector" which is copied:
body > table:nth-child(1) > tbody:nth-child(3) > tr:nth-child(6) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(2) > input:nth-child(1)
Here is the user name field when inspected:
<input name="userid_work" type="text" size="50" style="background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAASCAYAAABSO15qAAAAAXNSR0IArs4c6QAAAUBJREFUOBGVVE2ORUAQLvIS4gwzEysHkHgnkMiEc4zEJXCMNwtWTmDh3UGcYoaFhZUFCzFVnu4wIaiE+vvq6+6qTgthGH6O4/jA7x1OiCAIPwj7CoLgSXDxSjEVzAt9k01CBKdWfsFf/2WNuEwc2YqigKZpK9glAlVVwTTNbQJZlnlCkiTAZnF/mePB2biRdhwHdF2HJEmgaRrwPA+qqoI4jle5/8XkXzrCFoHg+/5ICdpm13UTho7Q9/0WnsfwiL/ouHwHrJgQR8WEwVG+oXpMPaDAkdzvd7AsC8qyhCiKJjiRnCKwbRsMw9hcQ5zv9maSBeu6hjRNYRgGFuKaCNwjkjzPoSiK1d1gDDecQobOBwswzabD/D3Np7AHOIrvNpHmPI+Kc2RZBm3bcp8wuwSIot7QQ0PznoR6wYSK0Xb/AGVLcWwc7Ng3AAAAAElFTkSuQmCC"); background-repeat: no-repeat; background-attachment: scroll; background-size: 16px 18px; background-position: 98% 50%; cursor: auto;" autocomplete="off">
I've tried various permutations of this, userid_work, etc, to no avail. Here's basiclly what I'm trying to do?
const USERNAME_SELECTOR = 'body > table:nth-child(1) > tbody:nth-child(3) > tr:nth-child(6) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(2) > input:nth-child(1)';
await page.click(USERNAME_SELECTOR);
await page.keyboard.type(CREDS.username);
So I was thinking of printing the form elements, and see what is returned. Unless someone can tell me how this user name field should be referenced?
Tried various permutations of this form element.
I don't know what you want to do (click or type)
So try this to click on the input:
await page.click('input[name="userid_work"]');
to type text into input:
await page.type('input[name="userid_work"]', text);
to click on the element using the javascript:
await page.$eval('input[name="userid_work"]', e => e.click);
to type into the element using the javascript:
await page.$eval('input[name="userid_work"]', e => e.value = 'text');

Resources