我尝试了这个简单的例子来点击一个链接,但是看起来它不起作用。 这个示例是在YouTube.com错误抛出中简单地单击登录按钮
(node:12304) UnhandledPromiseRejectionWarning: TypeError: youtubeLoginButton.evaluate is not a function
at C:\Users\GoodBoy\Desktop\code\Puppeteer\scrap-content-from-website\bot.js:17:30
at processTicksAndRejections (internal/process/task_queues.js:94:5)
(node:12304) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise
which was not handled with .catch(). (rejection id: 1)
(node:12304) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero
exit code.
const puppeteer = require('puppeteer')
;(async () => {
const browser = await puppeteer.launch({
headless: false,
defaultViewport: null,
args: ['--start-maximized'],
})
const page = await browser.newPage()
await page.goto('https://www.youtube.com/')
const youtubeLoginButtonXpath = '//*[@id="buttons"]/ytd-button-renderer/a'
const youtubeLoginButton = await page.$x(youtubeLoginButtonXpath)
await youtubeLoginButton.evaluate((form) => form.click())
await browser.close()
})()
这应该可以(我修改了一点XPath):
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://www.youtube.com',{ waitUntil: 'networkidle2' });
const elements = await page.$x('(//div[@id="buttons"]/ytd-button-renderer/a)[1]')
await elements[0].click()
await page.screenshot({path: 'full.png', fullPage: true});
await browser.close();
输出:登录页面截图(要上传一段新视频)