简体   繁体   English

如何获取特定元素<dl>在 puppeteer 中使用 querySelector

[英]How to get speciffic element to <dl> using querySelector in puppeteer

I'm begginer user from puppeteer, but cannot search this selector on code, can someone tell me how can I get the CSS selector of element which holds dl content in above HTML?我是 puppeteer 的初学者用户,但无法在代码上搜索此选择器,有人能告诉我如何获取在上述 HTML 中包含 dl 内容的元素的 CSS 选择器吗?

i'm trying to use selectorall and selector but only return undefined我正在尝试使用 selectorall 和 selector 但只返回 undefined

Code to trying get elements尝试获取元素的代码

await page.waitFor(".row > div:nth-child(2) > div");
  let list = await page.evaluate(() => {
    const getInnerTextForSelector = selector => {
      const element = document.querySelector(selector);
      if (element) return element.innerText;
      return "Selector not found";
    };

    return {
      title: getInnerTextForSelector('h5[class="card-title"]'),
      subtitle: getInnerTextForSelector(
        'h6[class="card-subtitle mb-2 text-muted"]'
      ),
      sit: getInnerTextForSelector("div > dt:nth-child(1)"),
    };
  });

Class to get.类得到。

<div class="col-lg">
<dl class="row">
<dt class="col-sm-3">Situação</dt><dd class="col-sm-9">Entrada</dd>
<dt class="col-sm-3">Trâmite</dt><dd class="col-sm-9">10/02/2020</dd>
<dt class="col-sm-3">Regime</dt><dd class="col-sm-9">Ordinário</dd>
<dt class="col-sm-3">Assunto</dt><dd class="col-sm-9">Diversos</dd>
<dt class="col-sm-3">Autor</dt>
<dd class="col-sm-9">Vereador<br>
<b>CLEITON MARCIO FOSSÁ</b>.
</dd>
</dl>
</div>

From what I understand, you can't unless you had some form of unique attribute such as id or data that can allow them to be distinguishable within the XML document.据我了解,除非您拥有某种形式的唯一属性(例如 id 或数据),可以允许它们在 XML 文档中被区分,否则您不能这样做。

If you want you could use a custom attribute like如果您愿意,可以使用自定义属性,例如

<dt class="col-sm-3" data-ref="Regime">Regime</dt>

and select it like so并像这样选择它

[data-ref="Regime"]

You could also use querySelectorAll("dt") and then use JS to iterate through the values to find the one one you want?您还可以使用 querySelectorAll("dt") 然后使用 JS 遍历这些值以找到您想要的值?

ps: little more on Selector Attributes , I know it says CSS but they work the same and gives a good visual sense of whats happening :) ps:关于Selector Attributes 的更多内容,我知道它说的是 CSS,但它们的工作方式相同,并且可以很好地了解正在发生的事情:)

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

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