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

Edu*_*cio 15 javascript java jquery selenium selenium-webdriver

我的javascript行:

$('#name').show();
Run Code Online (Sandbox Code Playgroud)

我的webdriver代码行:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("name"))).sendKeys("Some Name");
Run Code Online (Sandbox Code Playgroud)

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

WebDriverException: unknown error: cannot focus element
Run Code Online (Sandbox Code Playgroud)

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

Edu*_*cio 26

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

Actions actions = new Actions(driver);
actions.moveToElement(website);
actions.click();
actions.sendKeys("Some Name");
actions.build().perform();
Run Code Online (Sandbox Code Playgroud)

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


Dan*_*ous 12

派对有点晚了,但是那些在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()
Run Code Online (Sandbox Code Playgroud)

my_div您之前选择的元素在哪里,可能使用以下代码:

my_div = item.find_element_by_css_selector("div.foobar")
Run Code Online (Sandbox Code Playgroud)


Rag*_*air 7

在类似的行上,如果你使用量角器(angularjs),你可以这样使用它

actions = protractor.getInstance().actions();
actions.mouseMove(element);
actions.click();
actions.sendKeys("Some text");
actions.perform();`
Run Code Online (Sandbox Code Playgroud)