【问题标题】:How to get onFocus() to fire with Selenium如何让 onFocus() 使用 Selenium 触发
【发布时间】:2015-06-30 09:37:15
【问题描述】:

我正在尝试使用 Selenium 测试 HTML 表单。该页面通过在每个文本字段(这是HTML 中的输入标记)上使用javascript 的onFocus() 事件来验证输入数据。因此,我的代码需要确保每当我与文本字段交互时都会触发onFocus(),就像真正的用户填写表单一样。

我希望以下代码触发此事件:

this.textfield.clear();
if (value != null) {
    this.textfield.sendKeys(value);
}

但是,onFocus() 事件不会触发。我试过手动点击元素

this.textfield.click()

但这也行不通。我也尝试过使用 Actions 类

this.textfield.clear();
Actions action = new Actions(driver);
action.moveToElement(this.textfield).click().sendKeys(value.toString()).perform();

但这也没有给我带来任何运气。

Selenium 中有什么方法可以确保onFocus() 事件被执行?

【问题讨论】:

    标签: java javascript selenium selenium-webdriver


    【解决方案1】:

    您可以在浏览器会话中执行 JavaScript 以显式触发 onFocus()。这段代码在 C# 中,但在 Java 绑定中也必须有类似的东西。

    ((IJavaScriptExecutor)driver).ExecuteScript(string.Format("$('#{0}').onFocus()", elementId));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      相关资源
      最近更新 更多