Search code examples
javaseleniumwebdriversendkeys

Firefox did not get the return key


WebElement element = ...
element.clear();
element.sendKey("1234567\n");

Chrome got the return key, but Firefox did not. What is the different between "\n" and Keys.RETURN/Keys.ENTER?

 element.sendKey("1234567\t");

But Chrome did not get the TAB key.


Solution

  • When you hava a java String like "a\tb\nc", this becomes a sequence of characters with ASCII values 65, 9, 66, 10, 67. So, if you call element.sendKey("a\tb\nc");, then those bytes will be sent to the browser to do with as it will.

    In contrast, the Keys values are unicode, as can be seen in the source file at https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/Keys.java

    TAB          ('\uE004'),
    CLEAR        ('\uE005'),
    RETURN       ('\uE006'),
    ENTER        ('\uE007'),
    SHIFT        ('\uE008'),
    

    So clearly calling element.sendKey("a" + Keys.TAB + "b" + Keys.ENTER + "c"); will result in a different sequence of bytes being sent to the browser, and it would only be that sequence that standards obligate the browser to execute as you expect.