Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

My javascript line:

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

My webdriver code 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.

like image 879
Eduardo Fabricio Avatar asked Jan 14 '14 20:01

Eduardo Fabricio


2 Answers

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

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 ?

like image 94
Eduardo Fabricio Avatar answered Sep 19 '22 11:09

Eduardo Fabricio


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:

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_div is an element you've previously selected, perhaps with code like this:

my_div = item.find_element_by_css_selector("div.foobar")
like image 31
Daniel Porteous Avatar answered Sep 18 '22 11:09

Daniel Porteous