Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the expected behavior of puppeteer's ElementHandle.getProperty()?

Puppeteer 1.0.0-post. The getProperty() method seems somewhat magical. For example, if your page contains:

<a href="/foo/bar.html">link</a>

Then this will return not a relative but an absolute URL:

const propertyHandle = await elementHandle.getProperty('href');
const href = await propertyHandle.jsonValue();
// href is 'https://localhost:8080/foo/bar.html'

On the other hand, if you were to do the more roundabout:

const hrefHandle = await page.evaluateHandle(element => element.getAttribute('href'), elementHandle);
const href = await hrefHandle.jsonValue();
// href is '/foo/bar.html'

As far as I can tell, the puppeteer documentation doesn't mention this behavior of getProperty()?

It gets uglier, for instance if you want to get the style attribute of an element. It looks like puppeteer's getProperty() actually tries to parse the style in some way, which parsing is buggy/incomplete. The only way to get the raw text is with the roundabout call to evaluateHandle(...).

Is this an intended feature and simply a documentation bug? Or is it just, outright, a puppeteer bug?

Thanks.

like image 611
Dave Peck Avatar asked Feb 07 '18 00:02

Dave Peck


People also ask

What is ElementHandle in puppeteer?

ElementHandle prevents DOM element from garbage collection unless the handle is disposed . ElementHandles are auto-disposed when their origin frame gets navigated. ElementHandle instances can be used as arguments in page.

What is Page on in puppeteer?

The Puppeteer page class extends Node. js's native EventEmitter , which means that whenever you call page. on() , you are setting up an event listener using Node.


1 Answers

See HTML - attributes vs properties for difference between HTML attributes and DOM properties.

You can easily see the difference without Puppeteer, too. For example, on this page:

document.getElementById('nav-questions').href    
// returns "https://stackoverflow.com/questions"

document.getElementById('nav-questions').getAttribute('href')    
// returns "/questions"
like image 124
Pasi Avatar answered Sep 30 '22 07:09

Pasi