Attributes are additional information developers include in HTML tags. Attributes help in defining the characteristics of HTML elements.
Attributes are normally defined using "key-value" pairs. The key is the property that a developer wants to set. Let's consider a basic HTML tag with an attribute title.
<h3 class="red" id='3'> Some header </h3>
// here, class, id are keys, red, 3 are respective values
During the test script automation, we might need to fetch the attribute values of specific web elements to verify test scripts.
We can get the attribute using three ways in puppeteer:
We can get the attribute using getAttribute(attribute_name), getAttribute(attribute_name) method fetches the value of an attribute, in HTML code whatever is present in left side of '=' is an attribute, the value on the right side is the Attribute value.
This method works for all the attributes present in the HTML element
const puppeteer = require('puppeteer');
async function run(){
const browser = await puppeteer.launch({headless:false})
const page = await browser.newPage()
await page.goto('https://google.com')
var link = await page.$eval("a",
element=> element.getAttribute("href"))
await console.log(link)
}
run()

Similar to the getAttribute() funtion, we can use the .attribute function; with this way you cannot get all the sttribute but you can get only the standard attributes, like src, href, and few others.
const puppeteer = require('puppeteer');
async function run(){
const browser = await puppeteer.launch({headless:false})
const page = await browser.newPage()
await page.goto('https://google.com/');
var link = await page.$eval("a",
element=> element.href)
await console.log(link)
}
run()

You can get attribute from elementhandle using the getProperty().
const puppeteer = require('puppeteer');
async function run(){
const browser = await puppeteer.launch({headless:false})
const page = await browser.newPage()
await page.goto('https://google.com')
const element = await page.$("a");
const text = await (await element.getProperty("innerText")).jsonValue();
console.log(await text);
}
run()

const element = await page.$("a");
const classValue = await (await element.getProperty("class")).jsonValue();
console.log(await classValue );
const element = await page.$("a");
const dataValue = await (await element.getProperty("data")).jsonValue();
console.log(await dataValue);
const element = await page.$("a");
const value = await (await element.getProperty("value")).jsonValue();
console.log(await value );
const element = await page.$("a");
const style = await (await element.getProperty("style")).jsonValue();
console.log(await style);
const element = await page.$("a");
const id= await (await element.getProperty("id")).jsonValue();
console.log(await id);
const element = await page.$("a");
const ariaLabel= await (await element.getProperty("aria-label")).jsonValue();
console.log(await ariaLabel);
const element = await page.$("a");
const href = await (await element.getProperty("href")).jsonValue();
console.log(await href);
const element = await page.$("a");
const src= await (await element.getProperty("src")).jsonValue();
console.log(await src);
const element = await page.$("a");
const name= await (await element.getProperty("name")).jsonValue();
console.log(await name);
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.