question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I get element attribute without evaluate function

See original GitHub issue

Hi puppeteer folks!

I am using evaluate function for getting an attribute of web element.

Is there any way how to get attribute without evaluate?

For example for these attributes:

countdown innerText

My code with evaluate function:

const xpath = ....
const await this.activePage.$x(xpath)

const attributeValue =  await this.activePage.evaluate((obj, attr) => {
				const single = obj[attr]
				if (!single) return obj.getAttribute(attr)
				return single
			}, webElement, 'countdown')

I don`t want to use evaluate.

Thank you for any help!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

16reactions
vsemozhetbytcommented, Jan 16, 2019

Sorry, I missed out we can retrieve jsHandle for just an attribute via XPath. Try this:

'use strict';

const puppeteer = require('puppeteer');

(async function main() {
  try {
    const browser = await puppeteer.launch();
    const [page] = await browser.pages();

    await page.goto('https://example.org/');

    const [elementHandle] = await page.$x('.//a/@href');
    const propertyHandle = await elementHandle.getProperty('value');
    const propertyValue = await propertyHandle.jsonValue();

    console.log(propertyValue);

    await browser.close();
  } catch (err) {
    console.error(err);
  }
})();
http://www.iana.org/domains/example
10reactions
vsemozhetbytcommented, Jan 16, 2019

If the attributes have property counterparts, you can use something like this:

'use strict';

const puppeteer = require('puppeteer');

(async function main() {
  try {
    const browser = await puppeteer.launch();
    const [page] = await browser.pages();

    await page.goto('https://example.org/');

    const [elementHandle] = await page.$x('.//a');
    const propertyHandle = await elementHandle.getProperty('innerText');
    const propertyValue = await propertyHandle.jsonValue();

    console.log(propertyValue);

    await browser.close();
  } catch (err) {
    console.error(err);
  }
})();
More information...
Read more comments on GitHub >

github_iconTop Results From Across the Web

Get the Value of HTML Attributes Using Puppeteer
You can get attribute value with evaluate method. await page.evaluate('document.querySelector("span.styleNumber").
Read more >
Working with items and attributes - Amazon DynamoDB
A condition expression is a string containing attribute names, conditional operators, and built-in functions. The entire expression must evaluate to true.
Read more >
Puppeteer - Getting Element Attribute - Tutorialspoint
We can get attribute values of an element using Puppeteer. The attributes are added within the HTML tag. They are used to describe...
Read more >
XML - Robot Framework
Getting text or attributes of elements (e.g. Get Element Text and Get Element Attribute). Directly verifying text, attributes, or whole elements ...
Read more >
XSL Transformations (XSLT) Version 3.0 - W3C
[ERR XTSE0010] It is a static error if an XSLT-defined element is used in a context where it is not permitted, if a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found