mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
feat(Frame): introduce Frame.select
This patch adds `Frame.select` method that does the same functionality as former `Page.select`, but on a per-frame level. The `Page.select` method becomes a shortcut for the ÷main frame's select. Fixes #1139
This commit is contained in:
committed by
Andrey Lushnikov
parent
bc7f211474
commit
fede2643ac
@@ -406,6 +406,26 @@ class Frame {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} selector
|
||||
* @param {!Array<string>} values
|
||||
* @return {!Promise<!Array<string>>}
|
||||
*/
|
||||
async select(selector, ...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);
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {(string|number|Function)} selectorOrFunctionOrTimeout
|
||||
* @param {!Object=} options
|
||||
|
||||
12
lib/Page.js
12
lib/Page.js
@@ -784,17 +784,7 @@ class Page extends EventEmitter {
|
||||
* @return {!Promise<!Array<string>>}
|
||||
*/
|
||||
async select(selector, ...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);
|
||||
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);
|
||||
return this.mainFrame().select(selector, ...values);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user