Selenium onChange not working

家住魔仙堡 提交于 2019-12-18 04:44:05

问题


I have tried a number of things to try and get Selenium to pick up an 'onchange' event from a drop down menu, none of which has worked.

The offending HTML is:

<select onchange="doOpperation(this.options[this.selectedIndex]); this.selectedIndex = 0;" name="opps_ondemand" id="opps_ondemand">
  <option value="none" id="ondemand">Mark as...</option>
  <option cmd="blah1" value="add">Something</option>
  <option cmd="blah2" value="remove">None</option>
</select>

I have read that Selenium IDE doesn't record some on* events, and so it would be wise to use fireEvent():

$this->click("opps_ondemand");
$this->select("opps_ondemand", "label=Mark as...");
$this->click("//option[@value='add']");
sleep(3);
$this->fireEvent("//select[@id='opps_ondemand']", "change");

However, this does not work (with or without the fireEvent). I have also tried using

$this->fireEvent("locator", "click");

instead of

$this->click("locator");

but this did nothing.

Selenium does not complain about these locators not existing so I am assuming it can see the select/option elements fine. The problem seems to be the onChange event.

Does anyone know how to resolve this?

Thanks.


回答1:


I encountered exactly this problem, but in IE only (Firefox and Google Chrome works fine for me)

I found the solution to be manually forcing the update using JavaScript through Selenium's runScript. Some ways to do that can be found here:

How do I programmatically force an onchange event on an input?

For example, if I have jQuery in my Web page, I would do this:

$this->select('IDOfSelectElement', '*some label*');
$this->runScript("$('#IDOfSelectElement').trigger('change')");



回答2:


tried this?

$this->fireEvent("opps_ondemand", "onchange");

fireEvent(element_id, event_to_trigger);



来源:https://stackoverflow.com/questions/2544336/selenium-onchange-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!