Java jquery.show 和 WebDriverException 之后的元素:未知错误:无法聚焦元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21123539/
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-08-13 06:45:24  来源:igfitidea点击:

Element after jquery.show and WebDriverException: unknown error: cannot focus element

javajavascriptjqueryseleniumselenium-webdriver

提问by Eduardo Fabricio

My javascriptline:

我的javascript行:

$('#name').show();

My webdrivercode line:

我的网络驱动程序代码行:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("name"))).sendKeys("Some Name");

When I run the test it throws the following exception:

当我运行测试时,它抛出以下异常:

WebDriverException: unknown error: cannot focus element

So, I have been searching for a solution. There are some issues reported in chromium google code site. There are a lot of suggestions about using JavaScriptExecutor. But it doesn't seem a better solution for me, because it could make a browser dependent code.

所以,我一直在寻找解决方案。铬谷歌代码站点报告了一些问题。有很多关于使用的建议JavaScriptExecutor。但这对我来说似乎不是更好的解决方案,因为它可能使浏览器依赖代码。

采纳答案by Eduardo Fabricio

After some hours I finally found a solution by using Actions without JavascriptExecuter:

几个小时后,我终于通过使用不带 JavascriptExecuter 的 Actions 找到了解决方案:

Actions actions = new Actions(driver);
actions.moveToElement(website);
actions.click();
actions.sendKeys("Some Name");
actions.build().perform();

Well, it worked for me. However, is this way the better solution ?

嗯,它对我有用。但是,这种方式是更好的解决方案吗?

回答by Raghu K Nair

On the similar lines if you are using protractor (angularjs) you can use it this way `

在类似的线路上,如果您使用量角器(angularjs),您可以这样使用它`

actions = protractor.getInstance().actions();
actions.mouseMove(element);
actions.click();
actions.sendKeys("Some text");
actions.perform();`

回答by Daniel Porteous

A bit late to the party, but those looking for a solution to this problem while using selenium under python can use the following code:

聚会有点晚了,但是那些在 python 下使用 selenium 寻找解决此问题的方法的人可以使用以下代码:

actions = webdriver.ActionChains(driver)
actions.move_to_element(my_div)
actions.click()
actions.send_keys("Some name") # Replace with whichever keys you want.
actions.perform()

Where my_divis an element you've previously selected, perhaps with code like this:

my_div您之前选择的元素在哪里,可能是这样的代码:

my_div = item.find_element_by_css_selector("div.foobar")