Path: blob/trunk/py/selenium/webdriver/common/actions/key_actions.py
1864 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 ..utils import keys_to_typing20from .interaction import KEY, POINTER, WHEEL, Interaction21from .key_input import KeyInput22from .pointer_input import PointerInput23from .wheel_input import WheelInput242526class KeyActions(Interaction):27def __init__(self, source: KeyInput | PointerInput | WheelInput | None = None) -> None:28if source is None:29source = KeyInput(KEY)30self.input_source = source3132# Determine the correct source type string based on the input object33if isinstance(source, KeyInput):34source_type = KEY35elif isinstance(source, PointerInput):36source_type = POINTER37elif isinstance(source, WheelInput):38source_type = WHEEL39else:40source_type = KEY4142super().__init__(source_type)4344def key_down(self, letter: str) -> KeyActions:45return self._key_action("create_key_down", letter)4647def key_up(self, letter: str) -> KeyActions:48return self._key_action("create_key_up", letter)4950def pause(self, duration: int = 0) -> KeyActions:51return self._key_action("create_pause", duration)5253def send_keys(self, text: str | list) -> KeyActions:54if not isinstance(text, list):55text = keys_to_typing(text)56for letter in text:57self.key_down(letter)58self.key_up(letter)59return self6061def _key_action(self, action: str, letter) -> KeyActions:62meth = getattr(self.input_source, action)63meth(letter)64return self656667