events - Selenium onChange 不起作用

标签 events selenium onchange

我尝试了很多方法来尝试让 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/

相关文章:

Java : Can I keep information from event method through each call without using class static vars?

java - 通过父级的 id(带有 id)单击 span 按钮 - selenium with java

javascript - 模拟 - 旨在在 1 个节点上运行。 0 找到代替

jquery - 重复捕获 jQuery 更改事件以在同一索引上进行选择

javascript - 在 onChange 事件之后有条件地渲染子组件

javascript - 防止鼠标拖动时选择文本

Javascript ondblclick

objective-c - 如何获取 UIBarButtonItem 的当前索引以通过单击事件将其删除?

python - 将 Selenium 函数存储到变量中与不存储它有区别吗?

python-3.x - 使用 BeautifulSoup4 (Python3.x) 只能按 id 查找,不能按类查找