简体   繁体   English

如何使用 puppeteer 仅使用 class 在 Javascript 中 select 一个 div

[英]How can I select a div in Javascript with only a class using puppeteer

I am trying to setup a bot that will click a button on a webpage for a project.我正在尝试设置一个机器人,它将单击项目网页上的按钮。 Now this works on other webpages fine, however, on the page I want to use it on, the developers apparently thought it would be a brilliant idea to have almost no ids at all.现在这可以在其他网页上正常工作,但是,在我想使用它的页面上,开发人员显然认为几乎没有 id 将是一个绝妙的主意。 So I am stuck with trying to figure out how to select a div by its class (the only available selector it has).所以我一直试图弄清楚如何通过 class (它拥有的唯一可用选择器)来找出 select 一个 div。 Yes I have tried using Xpath: both times return null or an error.是的,我尝试过使用 Xpath:两次都返回 null 或错误。 Here is the working code if it is on another webpage:如果它在另一个网页上,这是工作代码:

const puppeteer = require('puppeteer');

async function scrapeProduct(url) {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(url);
  await page.click('#someid');

  await page.screenshot({ 
    path: "success.png", 
    fullPage: true
  });
  await browser.close();
}

scrapeProduct('someurl');

This is me testing Xpath which works but again this doesnt work for what I am wanting to do (unsure if this is the right way anyways - seems somewhat roundabout and I dont know how you would click with this):这是我测试 Xpath 的工作,但这对我想做的事情再次不起作用(不确定这是否是正确的方法 - 似乎有点迂回,我不知道你会如何点击这个):


async function scrapeProduct(url) {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(url);
  const [el] = await page.$x('//*[@id="btt-btn"]');
  const classID = await el.getProperty('textContent');
  const classIDText = await classID.jsonValue();
  console.log(classIDText);

  await page.screenshot({ 
    path: "success.png", 
    fullPage: true
  });
  await browser.close();
}

scrapeProduct('someurl');

The error I get talks about no node being found with that selector, which in this case is '.DropdownSelectInput__SelectBoxText-sc-1ssquc7-0 bnoarO'我得到的错误是关于没有找到该选择器的节点,在这种情况下是 '.DropdownSelectInput__SelectBoxText-sc-1ssquc7-0 bnoarO'

Does anyone have any thought?有没有人有任何想法? I have tried multiple different selectors and methods and cannot work out how I might do this.我尝试了多种不同的选择器和方法,但无法弄清楚我该如何做到这一点。

I had the similar issue before.我以前也有类似的问题。 Best way to select your div is following: select 您的 div 的最佳方法如下:

In the same HTML tree, select an element with a certain attribute.在同一个 HTML 树中,select 是一个具有一定属性的元素。 It can be an attribute name like data-at or id or anything that doesnt change at each load.它可以是属性名称,例如 data-at 或 id 或任何在每次加载时都不会改变的名称。 Once you manage to select that element, you can go up and down in the tree with element.parentNode or node.lastChild or node.firstChild etc. This would be the only way to do it.一旦你设法 select 那个元素,你可以 go 在树中上下使用 element.parentNode 或 node.lastChild 或 node.firstChild 等。这将是做到这一点的唯一方法。 Possibly website that you are trying to manipulate uses something that generate classes.您尝试操作的网站可能使用了生成类的东西。 So, no human being gave that classnames.所以,没有人给那个类名。

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

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