javascript 通过 WebDriver 执行的操作不会触发模糊事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16488619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:53:49  来源:igfitidea点击:

Actions through WebDriver will not trigger the blur event

javascriptjavawebdriverdom-eventsblur

提问by user973718

I have a web page 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.

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

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.

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

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

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

<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.

像我手动导航页面一样,我
在下拉列表中进行选择,然后在另一个字段中输入文本以将焦点从下拉列表中移开,以触发模糊事件。这没有用。我也试过跳出下拉菜单,也没有运气。

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.

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

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

Any suggestions? Thanks for your help.

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

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

编辑:我尝试在脚本字符串中添加“返回”。也没有用:

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

回答by bbbco

Here are a couple of ideas:

这里有几个想法:

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.

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

You can do this by using the sendKeys method:

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

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

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 returnyour code!

注入javascript来模拟blur方法。您已经尝试过它看起来像这样,但是您忘记了一个重要方面executeScript- 始终是return您的代码!

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.

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

UPDATE

更新

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);

回答by tkburbidge

Another option is to try triggering a click on something besides the input, like the body.

另一种选择是尝试触发对输入之外的其他东西的点击,比如身体。

driver.findElement(By.tagName("body")).click();

This is working for me.

这对我有用。

回答by Shai Azulay

Try using the Actions class, WebDriver support mouse and user interaction using this class Something like this:

尝试使用 Actions 类,WebDriver 支持使用此类的鼠标和用户交互 类似这样的:

WebElement element = driver.findElement(By.id('newOrder:shipToAddressType'));
(new Actions(driver)).moveToElement(element ,500,500).build().perform();

This will move as many pixels as you set it and trigger the blur event

这将移动您设置的尽可能多的像素并触发模糊事件