jquery.show和WebDriverException之后的元素:未知错误:无法聚焦元素
我的javascript行:
$('#name').show();
我的webdriver代码行:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("name"))).sendKeys("Some Name");
当我运行测试时,它会抛出以下exception:
WebDriverException: unknown error: cannot focus element
所以,我一直在寻找解决方案。 Chrome google代码网站报告了一些问题。 关于使用JavaScriptExecutor
有很多建议。 但它对我来说似乎不是一个更好的解决方案,因为它可以创建一个依赖于浏览器的代码。
几个小时后,我终于通过使用没有JavascriptExecuter的Actions找到了解决方案:
Actions actions = new Actions(driver); actions.moveToElement(website); actions.click(); actions.sendKeys("Some Name"); actions.build().perform();
嗯,它对我有用。 但是,这种方式是更好的解决方案吗?
派对有点晚了,但是那些在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()
my_div
是您之前选择的元素,可能使用以下代码:
my_div = item.find_element_by_css_selector("div.foobar")
在类似的行上,如果你使用量角器(angularjs),你可以这样使用它
actions = protractor.getInstance().actions(); actions.mouseMove(element); actions.click(); actions.sendKeys("Some text"); actions.perform();`