6

There's a bug in the chromedriver.exe Chrome driver for Selenium's WebDriver API. You can't use send_keys for certain types of inputs, like for the jQuery plugin "EZPZ Hint". It works okay on simple forms.

For now, I'm going to use Firefox and IE instead of Chrome to do my testing, but I'd like to see this issue fixed. Does anyone know of a different workaround that'd allow me to use Chrome instead of switching browsers?

Here's input had to use an image because it's generated dynamically and I couldn't copy/paste from Chrome's inspector thing.

I am using Chrome latest stable (14.0.835.202) and a Python script with a unittest class and nose as the test runner.

Here's the debugging info:

> Traceback (most recent call last):   File
> "\\server\QA\Automation\COMMON\product\common.py", line 35, in setUp
>     self.web.find_element_by_name("ezpz_hint_dummy_input").send_keys(self.user)
> File
> "C:\Python27\lib\site-packages\selenium-2.8.1-py2.7.egg\selenium\webdriver\remote\webelement.
> py", line 146, in send_keys
>     self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': typing})   File
> "C:\Python27\lib\site-packages\selenium-2.8.1-py2.7.egg\selenium\webdriver\remote\webelement.
> py", line 194, in _execute
>     return self._parent.execute(command, params)   File "C:\Python27\lib\site-packages\selenium-2.8.1-py2.7.egg\selenium\webdriver\remote\webdriver.p
> y", line 144, in execute
>     self.error_handler.check_response(response)   File "C:\Python27\lib\site-packages\selenium-2.8.1-py2.7.egg\selenium\webdriver\remote\errorhandle
> r.py", line 118, in check_response
>     raise exception_class(message, screen, stacktrace) WebDriverException: Message: 'Message: u\'focusElement execution
> failed;\\n Failed to send keys beca use cannot focus element\'
> \n-------------------- >> begin captured logging <<
> --------------------\ nselenium.webdriver.remote.remote_connection: DEBUG: POST http://127.0.0.1:51178/session {"sessionId ": null,
> "desiredCapabilities": {"platform": "ANY", "browserName": "chrome",
> "version": "", "javascr iptEnabled":
> true}}\nselenium.webdriver.remote.remote_connection: DEBUG: POST
> http://127.0.0.1:51178 /session/c85bcae35e0f07e805ea80c47ed9b75d/url
> {"url": "http://10.0.20.61/product", "sessionId": "c85bc
> ae35e0f07e805ea80c47ed9b75d"}\nselenium.webdriver.remote.remote_connection:
> DEBUG: POST http://127.0
> .0.1:51178/session/c85bcae35e0f07e805ea80c47ed9b75d/element {"using":
> "name", "sessionId": "c85bcae3 5e0f07e805ea80c47ed9b75d", "value":
> "ezpz_hint_dummy_input"}\nselenium.webdriver.remote.remote_conne
> ction: DEBUG: POST
> http://127.0.0.1:51178/session/c85bcae35e0f07e805ea80c47ed9b75d/element/:wdc:1319 220710066/value {"sessionId": "c85bcae35e0f07e805ea80c47ed9b75d",
> "id": ":wdc:1319220710066", "value ": ["send keys stuff here,
> redacted"]}\n--------------------- >> end captured logging <<
3
  • Did you try using javascript directly? (for instance: sendkeys). (I'm sorry, it's to late to elaborate the answer anymore or to test it myself.)
    – Alberto
    Nov 23, 2011 at 0:14
  • That might work, although the WebDriver people recommend against it here: code.google.com/p/selenium/wiki/…?
    – Aaron
    Nov 24, 2011 at 0:20
  • What happens if you execute .focus before .send_keys? Oct 3, 2012 at 0:09

6 Answers 6

1

The error message clearly sounds like sends_keys fails because for some reason it cannot set the focus to the input control prior to sending the actual keys to control. A possible work-around could be to click on the input first because this could possibly set the focus to the input control. Then calling sends_keys afterwards could succeed.

2
  • I have just started learning WebDriver recently, so I'm not sure how to do that. All I see is how to get the currently focused element (switch_to_active_element), not how to set focus.
    – Aaron
    Oct 24, 2011 at 18:35
  • 2
    .click() fails on the element. Firefox and IE can use ii. It's a bug in ChromeDriver.exe code.google.com/p/selenium/issues/detail?id=2328
    – Aaron
    Nov 22, 2011 at 19:02
1

There are a couple things I would try.

  • Get a different version of Selenium. I am using Selenium Webdriver 2.9 and can't reproduce the issue on the web sites that I am testing using the chrome driver.
  • Use Javascript to set the text of the element directly:

    WebElement element = ...;
    String script = "arguments[0].setAttribute('value', 'Set to this text.');"
    ((JavascriptExecutor) driver).executeScript(script, element);
    

There may be recommendations to not use javascript directly because this will a) not ensure the element is visible and active first b) not fire any events on the element, which is sometimes required to continue interacting with your page. If it is not required then it shouldn't matter. Unfortunately if you need to fire events such as onkeydown, onkeyup, onchange, etc, I have not found a simple way to do so with Selenium Webdriver.

1
  • 1
    In case anyone was looking, there is a pretty simple way to fire events with webdriver: "var evt = document.createEvent('MouseEvent'); evt.initMouseEvent('mousedown', true, true, window, 0, 0, 0, " + x + ", " + y + ", false, false, false, false, 0, null); arguments[0].dispatchEvent(evt); "
    – Sam Woods
    Apr 13, 2015 at 15:58
0

In this case the send-keys is not working as the focus event is not working . Hence firstly we can use executeScript for getting the focus on the element.Once the focus is there then it will be possible to use send-keys method provided by webdriver api.

So to get focus on the input you can do the following :

JavascriptExecutor js;
js.executeScript ("document.getElementById('x').focus()");

Once you add this line to your webdriver code (the focus will come on the element) then you just need to use send_keys.

0

This is solving issue with send keys (For IE 11 issue)

   IWebElement name= Driver.FindElement(By.Id("name"));
   executor.ExecuteScript("arguments[0].click();", name);
   name.SetAttribute("value","Text name");
-1

I just found that whenever you get this error message, it is an issue with the XPath. In Webdriver try to locate the element by its id: webdriverdriver.findElement(By.id("...")) and then use send keys.

2
  • Looking at the error, no XPath is used. Oct 3, 2012 at 0:14
  • A different error would be the result of not being able to find the web element.
    – user48956
    May 6, 2015 at 1:55
-1

click on element first then do sendkeys, and if .click() is not working you can click on the element through taking coordinates of the element on the page or click on it using robot

1
  • How does this answer the OP's question? The error is caused by a Chrome driver bug where send_keys does not work - and with a question this old, there's a good chance the question is no longer an issue.
    – Kate Paulk
    Feb 1, 2016 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.