admin管理员组文章数量:1304208
Banging my head against this wall for a bit, hoping someone may have some advice.
I am trying to invoke the onChange handler in a page loaded into Puppeteer. While I am able to select the appropriate option within the select (verified by turning off headless) I am unable to get the event handler to run.
1) Puppeteer provides the page.select() method, which explicitly states
"Triggers a change and input event once all the provided options have been selected"
I am able to select the expected option, but the event handler does not fire
await page.select('select#venue', '40');
2) Using native javascript inside a page.evaluate() call to manually select the required option, then generate a new Event and manually using element.dispatchEvent(event) e.g.
document.querySelector('select#venue > option:nth-child(4)').selected = "selected";
const event = new Event('change', {bubbles: true});
event.simulated = true;
document.querySelector('select#venue').dispatchEvent(event));
3) Using jQuery inside a page.evaluate() call to manually select the required otpion, then trigger the event e.g.
$("select#venue").val(17).change();
Completely out of ideas folks, and thoughts appreciated!
Banging my head against this wall for a bit, hoping someone may have some advice.
I am trying to invoke the onChange handler in a page loaded into Puppeteer. While I am able to select the appropriate option within the select (verified by turning off headless) I am unable to get the event handler to run.
1) Puppeteer provides the page.select() method, which explicitly states
"Triggers a change and input event once all the provided options have been selected"
I am able to select the expected option, but the event handler does not fire
await page.select('select#venue', '40');
2) Using native javascript inside a page.evaluate() call to manually select the required option, then generate a new Event and manually using element.dispatchEvent(event) e.g.
document.querySelector('select#venue > option:nth-child(4)').selected = "selected";
const event = new Event('change', {bubbles: true});
event.simulated = true;
document.querySelector('select#venue').dispatchEvent(event));
3) Using jQuery inside a page.evaluate() call to manually select the required otpion, then trigger the event e.g.
$("select#venue").val(17).change();
Completely out of ideas folks, and thoughts appreciated!
Share Improve this question asked Jul 2, 2018 at 21:16 Shaun HurleyShaun Hurley 3294 silver badges10 bronze badges 2- have you solved this? Am facing a similar issue.. – ziad.ali Commented Jul 27, 2018 at 13:14
-
How is this
select
generated? I mean, is it a native HTML element or is manipulated by a jQuery plugin or something like that? – Chris Commented Dec 19, 2019 at 13:25
1 Answer
Reset to default 9I was searching for same thing, to trigger an "onchange" event on a text field. I modified your code a little and it worked:
const myValue = 17;
await page.$eval('#selector', (element, myValue) => {
element.value = myValue;
const event = new Event('change');
element.dispatchEvent(event);
}, myValue);
本文标签: javascriptPuppeteer invoking onChange event handler not workingStack Overflow
版权声明:本文标题:javascript - Puppeteer invoking onChange event handler not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741746068a2395539.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论