简体   繁体   English

puppeteer 滚动并单击按钮

[英]puppeteer scroll and click on button

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:我正在使用 puppeteer 尝试截取网站的屏幕截图,但首先我必须按下一个名为“Lifetime”的按钮,其选择器是:

#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).我已经得到了我的代码,它会向下滚动到元素,但不会使用 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.到目前为止,我已经能够打开网页并向下滚动,但是当我尝试使用 page.click 时它不起作用。 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.com/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编辑:我将对页面进行全屏截图,然后使用 jimp 模块对其进行裁剪,因此主要是我必须弄清楚如何按下按钮

EDIT: im an idiot i was using the wrong selector my bad :)编辑:我是个白痴,我使用了错误的选择器,我很糟糕:)

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.com/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' })})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM