简体   繁体   English

使用 node.js puppeteer 定位自动生成的元素

[英]Targeting auto-generated elements with node.js puppeteer

I am using puppeteer for node.js v13.3.1, I am building a bot that will apply to jobs on linkedIn.我正在为 node.js v13.3.1 使用 puppeteer,我正在构建一个将应用于 linkedIn 上的工作的机器人。 So far I have the following code.到目前为止,我有以下代码。

const puppeteer = require('puppeteer');
   const SEARCHPARAM = "react" // change this for search param 
require("dotenv").config();
(async () => {
    const browser = await puppeteer.launch({headless:false});
  const page = await browser.newPage();
  await page.goto('https://www.linkedin.com/login?fromSignIn=true&trk=guest_homepage-basic_nav-header-signin')
  await page.setViewport({ width: 1920, height: 1080});
  await page.type("#username", process.env.NAMEUSERNAME)
  await page.type("#password", process.env.PASSWORD)
  await page.click('button[type="submit"]')
  //works fine
  await Promise.all([
    await page.waitForNavigation(), 
    await page.click('#global-nav > div > nav > ul > li:nth-child(3)'), 
    await page.type('input', SEARCHPARAM ), 
    await page.click('button[type="button"]'),
    await page.click('#ember1052 > span') //Will not hit the button
  ]);


})();

So far the bot is logging in and searching with the SEARCHPARAM just fine, however I am having trouble getting the bot to hit the easy apply button on the job posting.到目前为止,机器人可以正常登录并使用SEARCHPARAM进行搜索,但是我无法让机器人点击职位发布上的easy apply按钮。 Whenever I target this button I get the id as ember (number) and this number is never the same on different browser windows. I have tried targeting the button by class but node is telling me that it cannot find that selector.每当我定位此按钮时,我都会得到 id 作为ember (数字),并且这个数字在不同的浏览器 windows 上永远不会相同。我尝试通过 class 定位按钮,但节点告诉我它找不到该选择器。
Puppeteer docs for reference docs参考文档的 Puppeteer 文档

Please see this link for more detailed answer:请查看此链接以获得更详细的答案:
How to click element in Puppeteer using xPath如何使用 xPath 在 Puppeteer 中单击元素

const elements = await page.$x('<xPath>')
await elements[0].click() 

And your XPath selector should be like this:你的 XPath 选择器应该是这样的:

const element = await page.$x('//button/span[text()="Easy Apply"]')

Code syntax for:代码语法:
Button element that contain span element which has text inside "Easy Apply"

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

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