通过WebDriver执行的操作不会触发模糊事件 [英] Actions through WebDriver will not trigger the blur event

查看:213
本文介绍了通过WebDriver执行的操作不会触发模糊事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有两个下拉列表的网页。在一个下拉列表中选择一个选项将通过由blur事件触发的脚本更新另一个下拉列表中的选项列表。当焦点远离第一个下拉列表时,会触发模糊事件。这一切都可以在手动导航页面时正常工作。

I have a webpage with two dropdowns. Selecting an option in one dropdown will update the list of options in the other dropdown through a script that's triggered by the blur event. The blur event is triggered when the focus moves away from the first dropdown. This all works fine when navigating the page manually.

但是,当通过WebDriver执行相同的步骤时,模糊事件永远不会被触发,因此下拉列表永远不会更新,导致我的脚本失败。

However, when performing the same steps through WebDriver, the blur event is never triggered, and the dropdown is thus never updated, causing my script to fail.

这是我首先选择的下拉列表的html(以及附加了onblur脚本的html:

Here's the html for the dropdown I select first (and which has the onblur script attached to it:

<select id="newOrder:shipToAddressType" class="fieldRequired"     onblur="PrimeFaces.ab({source:this,event:'blur',process:'newOrder:odShipData',update:'newO>rder:odShipData',partialSubmit:true,oncomplete:function(xhr,status,args)>{focusOnShipToZip();;}}, arguments[1]);" tabindex="47" size="1" name="newOrder:shipToAddressType">
<option selected="selected" value="125">Domestic</option>
<option value="126">International</option>
<option value="127">Military</option>
</select>

这就是我所说的到目前为止:

Here's what I've tried so far:

我手动浏览页面
我在下拉列表中进行选择,然后在另一个文本中输入文本字段将焦点移离下拉列表以触发模糊事件。这没用。我也尝试跳出下拉列表,也没有运气。

Navigating the page as I would manually I make the selection in the dropdown, then enter text in another field to move the focus away from the dropdown in order to trigger the blur event. This did not work. I also tried tabbing out of the dropdown, also no luck.

执行javascript来触发模糊事件
我知道javascript是正确的,因为我可以从firebug成功运行它:它会触发第二个下拉列表的更新。但是,从WebDriver它似乎没有触发任何东西。

Executing javascript to trigger the blur event I know the javascript is correct, since I can successfully run it from firebug: it triggers an update of the second dropdown. However, from WebDriver it does not seem to trigger anything.

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('newOrder:shipToAddressType').blur()");

有什么建议吗?谢谢你的帮助。

Any suggestions? Thanks for your help.

编辑:
我尝试在脚本字符串中添加'return'。也没有用:

I tried adding 'return' to the script string. Also didn't work:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("return document.getElementById('newOrder:shipToAddressType').blur()");


推荐答案

以下是一些想法:

使用TAB转到下一个字段。这将模拟用户点击TAB键以继续下一个字段,理论上应该模拟浏览器的模糊事件。

Use a TAB to go to the next field. This would simulate a user hitting the TAB key to go on to the next field, and should theoretically simulate the browser's blur event.

您可以使用sendKeys方法执行此操作:

You can do this by using the sendKeys method:

WebElement element = driver.findElement(By.id('newOrder:shipToAddressType'));
element.sendKeys("\t");

注入javascript以模拟模糊方法。您已尝试尝试这看起来像,但你忘了 executeScript 的一个重要方面 - 总是返回你的代码!

Inject javascript to simulate the blur method. You have already attempted to try this it looks like, but you forgot one important aspect of executeScript -- always return your code!

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("return document.getElementById('newOrder:shipToAddressType').blur()");

最后一件事,包括您正在使用的浏览器,仅供参考之用,这将是有用的信息。

One last thing, it would be helpful info to include which browser your are using, just for informational purposes.

更新

尝试此操作直接关注元素,然后解散(模糊)它:

Try this to give direct focus to the element, and then unfocus (blur) it:

WebElement element = driver.findElement(By.id('newOrder:shipToAddressType'));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].focus(); arguments[0].blur(); return true", element);

这篇关于通过WebDriver执行的操作不会触发模糊事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆