Skip to content

Handle modifier keys in actions sendKeys #8179

@p0deje

Description

@p0deje

Problem

There is a certain confusion between send keys in actions vs element. Both allow to be used to simulate key chords (e.g. pressing a character key with a modifier key) but the behavior is very much different.

  • Calling send keys on the element with a key chord gives a character printed with the modifier key pressed
  • Calling send_keys on the actions with a key chord gives a character printed without the modifier key pressed

Should make the behave similar to avoid user confusion and work like OSS dialect used to (it didn't have this problem)?

Root cause

The problem is in the way actions sendKeys construct keyDown/keyUp events. It doesn't differentiate between modifier and character keys, so it simply generates a pair of keyDown/keyUp events for every key.

For example, if we wanted to press shift and a using actions sendKeys, it would generate the following events:

  • key down shift
  • key up shift
  • key down a
  • key up a

If we would like to make actions sendKeys work similar to element sendKeys, it should generate the events in a different order:

  • key down shift
  • key down a
  • key up a
  • key up shift

Sample Code

Here is a sample code using http://the-internet.herokuapp.com/inputs and tested in Chrome/Firefox:

Python

driver.find_element_by_tag_name('input').send_keys(Keys.SHIFT, 'a')
# types "A" to the input

driver.find_element_by_tag_name('input').click()
ActionChains(browser).send_keys(Keys.SHIFT, 'a').perform()
# types "a" to the input

Ruby

driver.find_element(tag_name: 'input').send_keys([:shift, 'a'])
# types "A" to the input

driver.find_element(tag_name: 'input').click
driver.action.send_keys([:shift, 'a']).perform
# types "a" to the input

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-needs decisionTLC needs to discuss and agreeC-dotnet.NET BindingsC-javaJava BindingsC-nodejsJavaScript BindingsC-pyPython BindingsC-rbRuby BindingsR-help wantedIssues looking for contributions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions