我尝试了很多方法来尝试让 Selenium 从下拉菜单中选择一个“onchange”事件,但都没有奏效。
有问题的 HTML 是:
<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>
我已经读到 Selenium IDE 不会记录一些 on* 事件,因此使用 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");
但是,这不起作用(有或没有 fireEvent)。我也试过使用
$this->fireEvent("locator", "click");
代替
$this->click("locator");
但这没有任何作用。
Selenium 不会提示这些定位器不存在,所以我假设它可以很好地看到选择/选项元素。问题似乎是 onChange 事件。
有谁知道如何解决这个问题?
谢谢。
最佳答案
我遇到了这个问题,但仅在 IE 中(Firefox 和 Google Chrome 对我来说很好用)
我发现解决方案是通过 Selenium 的 runScript
使用 JavaScript 手动强制更新。 .可以在此处找到一些方法:
How do I programmatically force an onchange event on an input?
例如,如果我的网页中有 jQuery,我会这样做:
$this->select('IDOfSelectElement', '*some label*');
$this->runScript("$('#IDOfSelectElement').trigger('change')");
关于events - Selenium onChange 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2544336/