mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
[api] Introduce Page.select method (#779)
This patch adds `Page.select` method to select values in a `select` tag.
This commit is contained in:
committed by
Andrey Lushnikov
parent
acdb588964
commit
45f264024b
23
lib/Page.js
23
lib/Page.js
@@ -693,6 +693,29 @@ class Page extends EventEmitter {
|
||||
await handle.dispose();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} selector
|
||||
* @param {!Array<string>} values
|
||||
*/
|
||||
async select(selector, ...values) {
|
||||
await this.$eval(selector, (element, values) => {
|
||||
if (element.nodeName.toLowerCase() !== 'select')
|
||||
throw new Error('Element is not a <select> element.');
|
||||
|
||||
const options = Array.from(element.options);
|
||||
|
||||
if (element.multiple) {
|
||||
for (const option of options)
|
||||
option.selected = values.includes(option.value);
|
||||
} else {
|
||||
element.value = values.shift();
|
||||
}
|
||||
|
||||
element.dispatchEvent(new Event('change'));
|
||||
element.dispatchEvent(new Event('input'));
|
||||
}, values);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} text
|
||||
* @param {{delay: (number|undefined)}=} options
|
||||
|
||||
Reference in New Issue
Block a user