2014-01-14 22 views
14

的JavaScript行:元jquery.show和WebDriverException後:未知錯誤:無法對焦元素

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

webdriver的代碼行:

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

當我運行測試引發以下異常:

WebDriverException: unknown error: cannot focus element 

因此,我一直在尋找解決方案。鉻代碼網站中報告了一些問題。有很多關於使用JavaScriptExecutor的建議。但對我來說這似乎並不是一個更好的解決方案,因爲它可以使瀏覽器的代碼變得更有用。

回答

26

一些小時之後,我終於找到了一個解決方案,通過使用操作不JavascriptExecuter:

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

好,它爲我工作。但是,這是更好的解決方案嗎?

+0

這也適用於我,也有類似的問題。 –

7

在類似的路線,如果你正在使用量角器(angularjs),你可以使用這種方式`

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

有點遲到了,但那些尋找一個解決這個問題,而使用下硒Python可以使用下面的代碼:

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") 
+1

只是爲了澄清,這個答案在Python 3.5中爲我工作,我認爲接受的解決方案是在Java –