mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
Implement ElementHandle.attribute() method (#543)
This patch implements ElementHandle.attribute() method to fetch a value of element's attribute.
This commit is contained in:
parent
74c53f3758
commit
77600c6c5e
16
docs/api.md
16
docs/api.md
@ -106,6 +106,7 @@
|
|||||||
+ [frame.waitForFunction(pageFunction[, options, ...args])](#framewaitforfunctionpagefunction-options-args)
|
+ [frame.waitForFunction(pageFunction[, options, ...args])](#framewaitforfunctionpagefunction-options-args)
|
||||||
+ [frame.waitForSelector(selector[, options])](#framewaitforselectorselector-options)
|
+ [frame.waitForSelector(selector[, options])](#framewaitforselectorselector-options)
|
||||||
* [class: ElementHandle](#class-elementhandle)
|
* [class: ElementHandle](#class-elementhandle)
|
||||||
|
+ [elementHandle.attribute(key)](#elementhandleattributekey)
|
||||||
+ [elementHandle.click([options])](#elementhandleclickoptions)
|
+ [elementHandle.click([options])](#elementhandleclickoptions)
|
||||||
+ [elementHandle.dispose()](#elementhandledispose)
|
+ [elementHandle.dispose()](#elementhandledispose)
|
||||||
+ [elementHandle.evaluate(pageFunction, ...args)](#elementhandleevaluatepagefunction-args)
|
+ [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 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])
|
#### elementHandle.click([options])
|
||||||
- `options` <[Object]>
|
- `options` <[Object]>
|
||||||
- `button` <[string]> `left`, `right`, or `middle`, defaults to `left`.
|
- `button` <[string]> `left`, `right`, or `middle`, defaults to `left`.
|
||||||
|
@ -103,6 +103,15 @@ class ElementHandle {
|
|||||||
const objectId = this._remoteObject.objectId;
|
const objectId = this._remoteObject.objectId;
|
||||||
return this._client.send('DOM.setFileInputFiles', { objectId, files });
|
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;
|
module.exports = ElementHandle;
|
||||||
|
@ -1180,6 +1180,12 @@ describe('Page', function() {
|
|||||||
}
|
}
|
||||||
expect(error.message).toContain('ElementHandle is disposed');
|
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() {
|
describe('ElementHandle.click', function() {
|
||||||
|
Loading…
Reference in New Issue
Block a user