Path: blob/trunk/py/selenium/webdriver/common/actions/key_actions.py
4049 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617from __future__ import annotations1819from selenium.webdriver.common.actions.interaction import KEY, Interaction20from selenium.webdriver.common.actions.key_input import KeyInput21from selenium.webdriver.common.actions.pointer_input import PointerInput22from selenium.webdriver.common.actions.wheel_input import WheelInput23from selenium.webdriver.common.utils import keys_to_typing242526class KeyActions(Interaction):27def __init__(self, source: KeyInput | PointerInput | WheelInput | None = None) -> None:28if source is None:29source = KeyInput(KEY)30self.input_source = source31super().__init__(source)3233def key_down(self, letter: str) -> KeyActions:34return self._key_action("create_key_down", letter)3536def key_up(self, letter: str) -> KeyActions:37return self._key_action("create_key_up", letter)3839def pause(self, duration: int = 0) -> KeyActions:40return self._key_action("create_pause", duration)4142def send_keys(self, text: str | list) -> KeyActions:43if not isinstance(text, list):44text = keys_to_typing(text)45for letter in text:46self.key_down(letter)47self.key_up(letter)48return self4950def _key_action(self, action: str, letter) -> KeyActions:51meth = getattr(self.source, action)52meth(letter)53return self545556