最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - puppeteer scroll and click on button - Stack Overflow

matteradmin5PV0评论

i am using puppeteer to try to take a screenshot of a website but first i have to press a button called "Lifetime" its selector is:

#profile > div.trn-profile.dtr-profile > div > div.content > div:nth-child(1) > div.material-card > a.btn.btn-season.selected

i have to first scroll down press the lifetime button and take a screenshot of the "solo", "duo" and "squad" stats like this:

target photo:

i am trying this in non-headless mode to make sure it works but it doesnt seem to be working. i have got my code to the point where it will scroll down to the element but not click it using page.click(SELECTOR). so far i have been able to open the webpage and scroll down but when i try to use page.click it does not work. i will worry about the screenshot later once i can figure out how to press the "lifetime" button

my code is:

var puppeteer = require('puppeteer');

let scrape = async () => {

const browser = await puppeteer.launch({
    headless: false
});

const page = await browser.newPage();

await page.goto('.BadGuyBen');
await page.tap('#profile > div.trn-profile.dtr-profile > div > div.content > div:nth-child(1) > div.material-card > a.btn.btn-season.selected');
await page.waitFor(2000);

await page.screenshot({
    path: 'stats.png',
    fullPage: true
})

browser.close();
};

scrape();

EDIT: i will take a fullscreen screenshot of the page then use jimp module to crop it so mainly i have to figure out how to press the button

EDIT: im an idiot i was using the wrong selector my bad :)

i am using puppeteer to try to take a screenshot of a website but first i have to press a button called "Lifetime" its selector is:

#profile > div.trn-profile.dtr-profile > div > div.content > div:nth-child(1) > div.material-card > a.btn.btn-season.selected

i have to first scroll down press the lifetime button and take a screenshot of the "solo", "duo" and "squad" stats like this:

target photo:

i am trying this in non-headless mode to make sure it works but it doesnt seem to be working. i have got my code to the point where it will scroll down to the element but not click it using page.click(SELECTOR). so far i have been able to open the webpage and scroll down but when i try to use page.click it does not work. i will worry about the screenshot later once i can figure out how to press the "lifetime" button

my code is:

var puppeteer = require('puppeteer');

let scrape = async () => {

const browser = await puppeteer.launch({
    headless: false
});

const page = await browser.newPage();

await page.goto('https://fortnitetracker./profile/pc/Twitch.BadGuyBen');
await page.tap('#profile > div.trn-profile.dtr-profile > div > div.content > div:nth-child(1) > div.material-card > a.btn.btn-season.selected');
await page.waitFor(2000);

await page.screenshot({
    path: 'stats.png',
    fullPage: true
})

browser.close();
};

scrape();

EDIT: i will take a fullscreen screenshot of the page then use jimp module to crop it so mainly i have to figure out how to press the button

EDIT: im an idiot i was using the wrong selector my bad :)

Share Improve this question edited Mar 29, 2018 at 9:06 Ben W asked Mar 28, 2018 at 16:34 Ben WBen W 411 gold badge1 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

i was using the wrong selector the one i should have been using is:

#profile > div.trn-profile.dtr-profile > div > div.content > div:nth-child(1) > div.material-card > a:nth-child(2)

so my code for people is:

var puppeteer = require('puppeteer');

let scrape = async () => {

const browser = await puppeteer.launch({
headless: false
});

const page = await browser.newPage();
var SELECTOR = "#profile > div.trn-profile.dtr-profile > div > div.content > div:nth-child(1) > div.material-card > a:nth-child(2)";

await page.goto('https://fortnitetracker./profile/pc/Twitch.BadGuyBen');
await page.focus(SELECTOR);
await page.waitFor(2000);
await page.click(SELECTOR);

await page.screenshot({
    path: 'stats.png',
    fullPage: true
})

browser.close();
};

scrape();
await page.$eval('section[class="promocode__segment"]', 
   e => {e.scrollIntoView({ behavior: 'smooth', block: 'end', inline: 'end' })})
Post a comment

comment list (0)

  1. No comments so far