简体   繁体   中英

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?

i'm trying to use selectorall and selector but only return 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.

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?

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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