SeleniumLibrary icon indicating copy to clipboard operation
SeleniumLibrary copied to clipboard

Press Key vs Press Keys

Open st0ff3ls opened this issue 4 years ago • 2 comments

In the past the keyword Press Key would allow you to submit an ASCII code to your identified locator. Example code :

Double Click Element | <yourlocatorxpath>
Press Key | <yourlocatorxpath> | \\08  ( =ascii for backspace )

Outcome was that the value of the xpath locator got cleared.

Press Key has been put deprecated, and got replaced by Press Keys, which uses the selenium common keys.

Example code :

Double Click Element | <yourlocatorxpath>
Press Keys | <yourlocatorxpath> | BACK_SPACE

Outcome now is that the value of the xpath locator gets selected, but the Press keys is however triggering a click in the xpath locator, which is removing the full selection, followed by the backspace ( which will only remove 1 char of the value of the locator ).

Trace Logging using deprecated Press key

20200407 15:05:47.194 : INFO : CLEARING FIELD 20200407 15:05:47.194 : TRACE : Return: None 20200407 15:05:47.197 : TRACE : Arguments: [ '//app-add-cashflow-credits//har-number-input//input' ] 20200407 15:05:47.197 : INFO : Double clicking element '//app-add-cashflow-credits//har-number-input//input'. 20200407 15:05:47.197 : DEBUG : POST http://127.0.0.1:64508/session/23f0d4da57a381d43e791171da87a45c/elements {"using": "xpath", "value": "//app-add-cashflow-credits//har-number-input//input"} 20200407 15:05:47.212 : DEBUG : http://127.0.0.1:64508 "POST /session/23f0d4da57a381d43e791171da87a45c/elements HTTP/1.1" 200 90 20200407 15:05:47.213 : DEBUG : Finished Request 20200407 15:05:47.214 : DEBUG : POST http://127.0.0.1:64508/session/23f0d4da57a381d43e791171da87a45c/actions {"actions": [{"type": "pointer", "parameters": {"pointerType": "mouse"}, "id": "mouse", "actions": [{"type": "pointerMove", "duration": 250, "x": 0, "y": 0, "origin": {"element-6066-11e4-a52e-4f735466cecf": "26d061ce-21e8-43f2-8b4f-7d5d4e0ca1d6"}}, {"type": "pointerDown", "duration": 0, "button": 0}, {"type": "pointerUp", "duration": 0, "button": 0}, {"type": "pointerDown", "duration": 0, "button": 0}, {"type": "pointerUp", "duration": 0, "button": 0}]}, {"type": "key", "id": "key", "actions": [{"type": "pause", "duration": 0}, {"type": "pause", "duration": 0}, {"type": "pause", "duration": 0}, {"type": "pause", "duration": 0}, {"type": "pause", "duration": 0}]}]} 20200407 15:05:47.759 : DEBUG : http://127.0.0.1:64508 "POST /session/23f0d4da57a381d43e791171da87a45c/actions HTTP/1.1" 200 14 20200407 15:05:47.760 : DEBUG : Finished Request 20200407 15:05:47.760 : TRACE : Return: None 20200407 15:05:47.762 : WARN : Keyword 'SeleniumLibrary.Press Key' is deprecated. use Press Keys instead. 20200407 15:05:47.763 : TRACE : Arguments: [ '//app-add-cashflow-credits//har-number-input//input' | '\08' ] 20200407 15:05:47.763 : DEBUG : POST http://127.0.0.1:64508/session/23f0d4da57a381d43e791171da87a45c/elements {"using": "xpath", "value": "//app-add-cashflow-credits//har-number-input//input"} 20200407 15:05:47.770 : DEBUG : http://127.0.0.1:64508 "POST /session/23f0d4da57a381d43e791171da87a45c/elements HTTP/1.1" 200 90 20200407 15:05:47.771 : DEBUG : Finished Request 20200407 15:05:47.771 : DEBUG : POST http://127.0.0.1:64508/session/23f0d4da57a381d43e791171da87a45c/element/26d061ce-21e8-43f2-8b4f-7d5d4e0ca1d6/value {"text": "\ue003", "value": ["\ue003"], "id": "26d061ce-21e8-43f2-8b4f-7d5d4e0ca1d6"} 20200407 15:05:47.804 : DEBUG : http://127.0.0.1:64508 "POST /session/23f0d4da57a381d43e791171da87a45c/element/26d061ce-21e8-43f2-8b4f-7d5d4e0ca1d6/value HTTP/1.1" 200 14 20200407 15:05:47.805 : DEBUG : Finished Request 20200407 15:05:47.805 : TRACE : Return: None 20200407 15:05:47.807 : TRACE : Arguments: [ 'END CLEARING FIELD' ] 20200407 15:05:47.807 : INFO : END CLEARING FIELD

Tracelogging when using Press Keys :

20200407 15:03:25.181 : INFO : CLEARING FIELD 20200407 15:03:25.181 : TRACE : Return: None 20200407 15:03:25.184 : TRACE : Arguments: [ '//app-add-cashflow-credits//har-number-input//input' ] 20200407 15:03:25.185 : INFO : Double clicking element '//app-add-cashflow-credits//har-number-input//input'. 20200407 15:03:25.185 : DEBUG : POST http://127.0.0.1:63805/session/5e51e5a92dd00e05cd4818ba32d9a768/elements {"using": "xpath", "value": "//app-add-cashflow-credits//har-number-input//input"} 20200407 15:03:25.202 : DEBUG : http://127.0.0.1:63805 "POST /session/5e51e5a92dd00e05cd4818ba32d9a768/elements HTTP/1.1" 200 90 20200407 15:03:25.203 : DEBUG : Finished Request 20200407 15:03:25.204 : DEBUG : POST http://127.0.0.1:63805/session/5e51e5a92dd00e05cd4818ba32d9a768/actions {"actions": [{"type": "pointer", "parameters": {"pointerType": "mouse"}, "id": "mouse", "actions": [{"type": "pointerMove", "duration": 250, "x": 0, "y": 0, "origin": {"element-6066-11e4-a52e-4f735466cecf": "5fabb1a2-a19e-4787-beec-1ddb256f8a62"}}, {"type": "pointerDown", "duration": 0, "button": 0}, {"type": "pointerUp", "duration": 0, "button": 0}, {"type": "pointerDown", "duration": 0, "button": 0}, {"type": "pointerUp", "duration": 0, "button": 0}]}, {"type": "key", "id": "key", "actions": [{"type": "pause", "duration": 0}, {"type": "pause", "duration": 0}, {"type": "pause", "duration": 0}, {"type": "pause", "duration": 0}, {"type": "pause", "duration": 0}]}]} 20200407 15:03:25.816 : DEBUG : http://127.0.0.1:63805 "POST /session/5e51e5a92dd00e05cd4818ba32d9a768/actions HTTP/1.1" 200 14 20200407 15:03:25.817 : DEBUG : Finished Request 20200407 15:03:25.817 : TRACE : Return: None 20200407 15:03:25.821 : TRACE : Arguments: [ '//app-add-cashflow-credits//har-number-input//input' | 'BACKSPACE' ] 20200407 15:03:25.822 : INFO : Sending key(s) ('BACKSPACE',) to //app-add-cashflow-credits//har-number-input//input element. 20200407 15:03:25.822 : DEBUG : POST http://127.0.0.1:63805/session/5e51e5a92dd00e05cd4818ba32d9a768/elements {"using": "xpath", "value": "//app-add-cashflow-credits//har-number-input//input"} 20200407 15:03:25.851 : DEBUG : http://127.0.0.1:63805 "POST /session/5e51e5a92dd00e05cd4818ba32d9a768/elements HTTP/1.1" 200 90 20200407 15:03:25.852 : DEBUG : Finished Request 20200407 15:03:25.853 : DEBUG : POST http://127.0.0.1:63805/session/5e51e5a92dd00e05cd4818ba32d9a768/actions {"actions": [{"type": "pointer", "parameters": {"pointerType": "mouse"}, "id": "mouse", "actions": [{"type": "pointerMove", "duration": 250, "x": 0, "y": 0, "origin": {"element-6066-11e4-a52e-4f735466cecf": "5fabb1a2-a19e-4787-beec-1ddb256f8a62"}}, {"type": "pointerDown", "duration": 0, "button": 0}, {"type": "pointerUp", "duration": 0, "button": 0}]}, {"type": "key", "id": "key", "actions": [{"type": "pause", "duration": 0}, {"type": "pause", "duration": 0}, {"type": "pause", "duration": 0}]}]} 20200407 15:03:26.393 : DEBUG : http://127.0.0.1:63805 "POST /session/5e51e5a92dd00e05cd4818ba32d9a768/actions HTTP/1.1" 200 14 20200407 15:03:26.394 : DEBUG : Finished Request 20200407 15:03:26.395 : INFO : Pressing special key BACKSPACE to element. 20200407 15:03:26.395 : INFO : Releasing special key BACKSPACE. 20200407 15:03:26.396 : DEBUG : POST http://127.0.0.1:63805/session/5e51e5a92dd00e05cd4818ba32d9a768/actions {"actions": [{"type": "pointer", "parameters": {"pointerType": "mouse"}, "id": "mouse", "actions": [{"type": "pause", "duration": 0}, {"type": "pause", "duration": 0}, {"type": "pause", "duration": 0}]}, {"type": "key", "id": "key", "actions": [{"type": "keyDown", "value": "\ue003"}, {"type": "keyUp", "value": "\ue003"}, {"type": "keyUp", "value": "\ue003"}]}]} 20200407 15:03:26.418 : DEBUG : http://127.0.0.1:63805 "POST /session/5e51e5a92dd00e05cd4818ba32d9a768/actions HTTP/1.1" 200 14 20200407 15:03:26.478 : DEBUG : Finished Request 20200407 15:03:26.478 : TRACE : Return: None 20200407 15:03:26.482 : TRACE : Arguments: [ 'END CLEARING FIELD' ] 20200407 15:03:26.482 : INFO : END CLEARING FIELD

Python 3.7 Seleniumlib 4.3.0 RobotFW 3.1.2 macOs 10.13 Chrome 80

st0ff3ls avatar Apr 07 '20 12:04 st0ff3ls

Yes, the above is true. Underlying Selenium provides helpers which sets the focus to element by clicking the element. I did not have clear opinion how the keyword should work and decided to use the available helpers. Now when looking back that decision, it's pretty clear that the decision was not the best one. Perhaps it would be best investigate other ways to set the focus and try to find something that would work better in normal usage.

Would you be interested investigate and perhaps provide PR to change the keyword?

aaltat avatar Apr 07 '20 17:04 aaltat

There is a workaround for this, based on the current code since Press Keys can be given a locator of None and in that case no click happens, so instead of:

Double Click Element | <yourlocatorxpath>
Press Keys | <yourlocatorxpath> | BACK_SPACE

You could do:

Double Click Element | <yourlocatorxpath>
Press Keys | None | BACK_SPACE

I did not check but another way to focus an element is sending Keys.NULL key on that element instead of clicking it (ActionChains(self.driver).click(element).perform()) but right from the top of my head I don't know if that would remove the full selection, it might be worth taking a look at that.

flowrean-marcus avatar Apr 28 '21 18:04 flowrean-marcus