Warm tip: This article is reproduced from serverfault.com, please click

How do i scrape the value of a element tag with puppeteer

发布于 2020-11-29 00:29:11
    <button class="button width-full button--primary" data-automation-id="signin-submit-btn" data-tl-id="signin-submit-btn" type="submit"><span class="button-wrapper">Sign in</span></button>

I need to scrape the value of "data-automation-id" with puppeteer which would be "signin-submit-btn". I know that I can grab the text by doing this

document.querySelector('button[class="button width-full button--primary"]').innerText;

but I need to know how to grab that value of "data-automation-id"

Questioner
falcon
Viewed
0
Marc 2020-11-29 08:32:55

It looks like you're trying to capture the value of a Data Attribute. You can do it by referencing the button element's dataset like this:

let mybutton = document.querySelector('button[class="button width-full button--primary"]');
let autoId = mybutton.dataset.automationId;
console.log(autoId);
<button class="button width-full button--primary" data-automation-id="signin-submit-btn" data-tl-id="signin-submit-btn" type="submit"><span class="button-wrapper">Sign in</span></button>

Reference here: https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes