New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hwo can I set HTML select element value? #613
Comments
@thujikun I don't follow, why the second approach with |
@aslushnikov I 'm using React. And I think |
Also a lot of websites use jQuery 🤒 And have code like |
@sean-hill By firing the change event manually. As far as React goes. It seems like your component would need to expose an API to programatically change the value. Just like Could someone provide a react-based sample test though? That would help loads in understanding the exact situation and how to resolve it. |
Doesn't this
actually just circumvent the underlying issue, that you can't click on certain elements? Will there be support for this in the future? I think this circumvention is more of an escape hatch than it should be a default approach. |
@thujikun I had to deal with the same issue and this seems to work for me. A bit of a hack though:
|
@fixingcredit |
@thujikun the |
@aslushnikov |
Incomplete polyfill for if (page.select === undefined) {
page.select = async function(selector, value) {
await page.evaluate((selector, value) => {
let element = document.querySelector(selector);
element.querySelector('option[value="' + value + '"]').selected = true;
let event = new Event('change', { bubbles: true });
event.simulated = true;
element.dispatchEvent(event);
}, selector, value);
};
} |
@aslushnikov Doesn't this select method trigger change event on selecting value? |
@thujikun yes it does |
Can I suggest adding something to the page.select function? The code I added was: This is the altered select function: |
@AlexChung1995 feel free to send a PR or file a new issue, we'll discuss it there |
I would extend also ElementHandle: /**
* Set value on a select element
* @param {string} value
* @returns {Promise<Undefined>}
*/
ElementHandle.prototype.select = async function( value ) {
await this._page.evaluateHandle( ( el, value ) => {
const event = new Event( "change", { bubbles: true });
event.simulated = true;
el.querySelector( `option[value="${ value }"]` ).selected = true;
el.dispatchEvent( event );
}, this, value );
}; Then one can use it like: const combobox= await page.$('select');
await combobox.select("optionFoo"); |
When selecting the product's options (for configurable products with no swatches), we should then dispatch a "change" and "input" events. This is implemented here in Puppeteer : https://github.com/puppeteer/puppeteer/blob/dd470c7a226a8422a938a7b0fffa58ffc6b78512/src/common/JSHandle.ts#L597 You can also find more info here : puppeteer/puppeteer#613 (comment)
When selecting the product's options (for configurable products with no swatches), we should then dispatch a "change" and "input" events. This is implemented here in Puppeteer : https://github.com/puppeteer/puppeteer/blob/dd470c7a226a8422a938a7b0fffa58ffc6b78512/src/common/JSHandle.ts#L597 You can also find more info here : puppeteer/puppeteer#613 (comment)
I tried following code. But I cannot set the value.
And I set the value with page.evaluate.
But I need to virtual DOM value.
So following code is not useful for me.
The text was updated successfully, but these errors were encountered: