Implement ElementHandle.attribute() method (#543)

This patch implements ElementHandle.attribute() method to fetch a value of
element's attribute.
This commit is contained in:
Batiste Bieler 2017-08-29 20:24:32 +02:00 committed by Andrey Lushnikov
parent 74c53f3758
commit 77600c6c5e
3 changed files with 31 additions and 0 deletions

View File

@ -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)
@ -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`.

View File

@ -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;

View File

@ -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() {