Skip to content

Commit

Permalink
Implement ElementHandle.attribute() method (puppeteer#543)
Browse files Browse the repository at this point in the history
This patch implements ElementHandle.attribute() method to fetch a value of
element's attribute.
  • Loading branch information
Batiste Bieler authored and aslushnikov committed Aug 29, 2017
1 parent 74c53f3 commit 77600c6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
+ [frame.waitForFunction(pageFunction[, options, ...args])](#framewaitforfunctionpagefunction-options-args)
+ [frame.waitForSelector(selector[, options])](#framewaitforselectorselector-options)
* [class: ElementHandle](#class-elementhandle)
+ [elementHandle.attribute(key)](#elementhandleattributekey)
+ [elementHandle.click([options])](#elementhandleclickoptions)
+ [elementHandle.dispose()](#elementhandledispose)
+ [elementHandle.evaluate(pageFunction, ...args)](#elementhandleevaluatepagefunction-args)
Expand Down Expand Up @@ -1189,6 +1190,21 @@ puppeteer.launch().then(async browser => {

ElementHandle prevents DOM element from garbage collection unless the handle is [disposed](#elementhandledispose). ElementHandles are auto-disposed when their origin frame gets navigated.

#### elementHandle.attribute(key)
- `key` <string> the name the attribute of this Element.
- returns: <[Promise]>

```js
const puppeteer = require('puppeteer');

puppeteer.launch().then(async browser => {
const page = await browser.newPage();
await page.goto('https://google.com');
const inputElement = await page.$('input');
const inputType = await inputElement.attribute('type');
});
```

#### elementHandle.click([options])
- `options` <[Object]>
- `button` <[string]> `left`, `right`, or `middle`, defaults to `left`.
Expand Down
9 changes: 9 additions & 0 deletions lib/ElementHandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ class ElementHandle {
const objectId = this._remoteObject.objectId;
return this._client.send('DOM.setFileInputFiles', { objectId, files });
}

/**
* @param {!<string} key
* @return {!Promise}
*/
async attribute(key) {
return await this.evaluate((element, key) => element.getAttribute(key), key);
}

}

module.exports = ElementHandle;
Expand Down
6 changes: 6 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,12 @@ describe('Page', function() {
}
expect(error.message).toContain('ElementHandle is disposed');
}));
it('should return attribute', SX(async function() {
await page.setContent('<section id="testAttribute">43543</section>');
const element = await page.$('section');
expect(element).toBeTruthy();
expect(await element.attribute('id')).toBe('testAttribute');
}));
});

describe('ElementHandle.click', function() {
Expand Down

0 comments on commit 77600c6

Please sign in to comment.