Java 如何使用Selenium启动onFocus()
我正在尝试使用Selenium测试Java 如何使用Selenium启动onFocus(),java,javascript,selenium,selenium-webdriver,Java,Javascript,Selenium,Selenium Webdriver,我正在尝试使用Selenium测试HTML表单。页面通过在每个文本字段上使用javascript的onFocus()事件(这是HTML中的输入标记)来验证输入数据。因此,每当我与文本字段交互时,我的代码都需要确保onFocus()被解雇,就像真实用户填写表单一样 我希望以下代码触发此事件: this.textfield.clear(); if (value != null) { this.textfield.sendKeys(value); } 但是,onFocus()事件不会触发。我
HTML
表单。页面通过在每个文本字段上使用javascript的onFocus()
事件(这是HTML
中的输入标记)来验证输入数据。因此,每当我与文本字段交互时,我的代码都需要确保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()
事件?您可以在浏览器会话中执行JavaScript来显式触发onFocus()。这段代码是用C#编写的,但Java绑定中也一定有类似的东西
((IJavaScriptExecutor)driver).ExecuteScript(string.Format("$('#{0}').onFocus()", elementId));