mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
feat(Page.select): return selected options from Page.select (#1099)
This patch teaches Page.select to return an array of actually selected options. If no option is selected, an empty array will be returned.
This commit is contained in:
committed by
Andrey Lushnikov
parent
9f19641a3d
commit
e70f98ddb9
15
lib/Page.js
15
lib/Page.js
@@ -779,22 +779,19 @@ class Page extends EventEmitter {
|
||||
/**
|
||||
* @param {string} selector
|
||||
* @param {!Array<string>} values
|
||||
* @return {!Promise<!Array<string>>}
|
||||
*/
|
||||
async select(selector, ...values) {
|
||||
await this.$eval(selector, (element, values) => {
|
||||
return 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.value = undefined;
|
||||
for (const option of options)
|
||||
option.selected = values.includes(option.value);
|
||||
element.dispatchEvent(new Event('input', { 'bubbles': true }));
|
||||
element.dispatchEvent(new Event('change', { 'bubbles': true }));
|
||||
return options.filter(option => option.selected).map(option => option.value);
|
||||
}, values);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user