Javascript 如何通过xpath选择元素并使用Puppeter单击它?

Javascript 如何通过xpath选择元素并使用Puppeter单击它?,javascript,node.js,xpath,automation,puppeteer,Javascript,Node.js,Xpath,Automation,Puppeteer,我尝试了这个简单的例子来点击一个链接,但它似乎不起作用。 例如,在youtube.com上点击一个登录按钮 错误抛出 (node:12304) UnhandledPromiseRejectionWarning: TypeError: youtubeLoginButton.evaluate is not a function at C:\Users\GoodBoy\Desktop\code\Puppeteer\scrape-content-from-website\bot.js:17:30

我尝试了这个简单的例子来点击一个链接,但它似乎不起作用。 例如,在youtube.com上点击一个登录按钮 错误抛出

(node:12304) UnhandledPromiseRejectionWarning: TypeError: youtubeLoginButton.evaluate is not a function
    at C:\Users\GoodBoy\Desktop\code\Puppeteer\scrape-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 puppeter=require('puppeter')
;(异步()=>{
const browser=wait puppeter.launch({
无头:错,
defaultViewport:null,
参数:['--开始最大化'],
})
const page=wait browser.newPage()
等待页面。转到('https://www.youtube.com/')
const youtubeLoginButtonXpath='/*[@id=“buttons”]/ytd按钮渲染器/a'
const youtubeLoginButton=等待页面$x(youtubeLoginButtonXpath)
等待youtubeLoginButton.evaluate((表单)=>form.click())
等待浏览器关闭()
})()
这应该行得通(我对您的XPath做了一些修改):

输出:登录页面截图(上传新视频)

这应该行得通(我对您的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();