diff --git a/DrissionPage/__init__.py b/DrissionPage/__init__.py index 0e9b576..6859004 100644 --- a/DrissionPage/__init__.py +++ b/DrissionPage/__init__.py @@ -14,4 +14,4 @@ from ._configs.chromium_options import ChromiumOptions from ._configs.session_options import SessionOptions __all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__'] -__version__ = '4.0.4.13' +__version__ = '4.0.4.14' diff --git a/DrissionPage/_elements/chromium_element.py b/DrissionPage/_elements/chromium_element.py index dda5ea0..62c3179 100644 --- a/DrissionPage/_elements/chromium_element.py +++ b/DrissionPage/_elements/chromium_element.py @@ -11,7 +11,7 @@ from pathlib import Path from re import search from time import perf_counter, sleep -from DataRecorder.tools import get_usable_path +from DataRecorder.tools import get_usable_path, make_valid_name from .none_element import NoneElement from .session_element import make_session_ele @@ -569,8 +569,11 @@ class ChromiumElement(DrissionElement): if src.lower().startswith('data:image'): r = search(r'data:image/(.*?);base64,', src) name = f'img.{r.group(1)}' if r else None - name = name or basename(self.property('currentSrc')) - path = get_usable_path(f'{path}{sep}{name}').absolute() + path = Path(path) / make_valid_name(name or basename(self.property('currentSrc'))) + if not path.suffix: + path = path.with_suffix('.jpg') + path.parent.mkdir(parents=True, exist_ok=True) + path = path.absolute() write_type = 'wb' if isinstance(data, bytes) else 'w' with open(path, write_type) as f: diff --git a/DrissionPage/_functions/keys.py b/DrissionPage/_functions/keys.py index e4bebea..d55267c 100644 --- a/DrissionPage/_functions/keys.py +++ b/DrissionPage/_functions/keys.py @@ -10,6 +10,12 @@ from ..errors import AlertExistsError class Keys: """特殊按键""" + CTRL_A = ('\ue009', 'a') + CTRL_C = ('\ue009', 'c') + CTRL_X = ('\ue009', 'x') + CTRL_V = ('\ue009', 'v') + CTRL_Z = ('\ue009', 'z') + CTRL_Y = ('\ue009', 'y') NULL = '\ue000' CANCEL = '\ue001' # ^break @@ -94,35 +100,108 @@ keyDefinitions = { '7': {'keyCode': 55, 'key': '7', 'code': 'Digit7'}, '8': {'keyCode': 56, 'key': '8', 'code': 'Digit8'}, '9': {'keyCode': 57, 'key': '9', 'code': 'Digit9'}, - 'Power': {'key': 'Power', 'code': 'Power'}, - 'Eject': {'key': 'Eject', 'code': 'Eject'}, - '\ue001': {'keyCode': 3, 'code': 'Abort', 'key': 'Cancel'}, - '\ue002': {'keyCode': 6, 'code': 'Help', 'key': 'Help'}, - '\ue003': {'keyCode': 8, 'code': 'Backspace', 'key': 'Backspace'}, - '\ue004': {'keyCode': 9, 'code': 'Tab', 'key': 'Tab'}, - '\ue005': {'keyCode': 12, 'shiftKeyCode': 101, 'key': 'Clear', 'code': 'Numpad5', 'shiftKey': '5', 'location': 3}, - '\ue006': {'keyCode': 13, 'code': 'NumpadEnter', 'key': 'Enter', 'text': '\r', 'location': 3}, - '\ue007': {'keyCode': 13, 'code': 'Enter', 'key': 'Enter', 'text': '\r'}, - '\r': {'keyCode': 13, 'code': 'Enter', 'key': 'Enter', 'text': '\r'}, + 'a': {'keyCode': 65, 'key': 'a', 'code': 'KeyA'}, + 'b': {'keyCode': 66, 'key': 'b', 'code': 'KeyB'}, + 'c': {'keyCode': 67, 'key': 'c', 'code': 'KeyC'}, + 'd': {'keyCode': 68, 'key': 'd', 'code': 'KeyD'}, + 'e': {'keyCode': 69, 'key': 'e', 'code': 'KeyE'}, + 'f': {'keyCode': 70, 'key': 'f', 'code': 'KeyF'}, + 'g': {'keyCode': 71, 'key': 'g', 'code': 'KeyG'}, + 'h': {'keyCode': 72, 'key': 'h', 'code': 'KeyH'}, + 'i': {'keyCode': 73, 'key': 'i', 'code': 'KeyI'}, + 'j': {'keyCode': 74, 'key': 'j', 'code': 'KeyJ'}, + 'k': {'keyCode': 75, 'key': 'k', 'code': 'KeyK'}, + 'l': {'keyCode': 76, 'key': 'l', 'code': 'KeyL'}, + 'm': {'keyCode': 77, 'key': 'm', 'code': 'KeyM'}, + 'n': {'keyCode': 78, 'key': 'n', 'code': 'KeyN'}, + 'o': {'keyCode': 79, 'key': 'o', 'code': 'KeyO'}, + 'p': {'keyCode': 80, 'key': 'p', 'code': 'KeyP'}, + 'q': {'keyCode': 81, 'key': 'q', 'code': 'KeyQ'}, + 'r': {'keyCode': 82, 'key': 'r', 'code': 'KeyR'}, + 's': {'keyCode': 83, 'key': 's', 'code': 'KeyS'}, + 't': {'keyCode': 84, 'key': 't', 'code': 'KeyT'}, + 'u': {'keyCode': 85, 'key': 'u', 'code': 'KeyU'}, + 'v': {'keyCode': 86, 'key': 'v', 'code': 'KeyV'}, + 'w': {'keyCode': 87, 'key': 'w', 'code': 'KeyW'}, + 'x': {'keyCode': 88, 'key': 'x', 'code': 'KeyX'}, + 'y': {'keyCode': 89, 'key': 'y', 'code': 'KeyY'}, + 'z': {'keyCode': 90, 'key': 'z', 'code': 'KeyZ'}, + ' ': {'keyCode': 32, 'key': ' ', 'code': 'Space'}, + '*': {'keyCode': 106, 'key': '*', 'code': 'NumpadMultiply', 'location': 3}, + '+': {'keyCode': 107, 'key': '+', 'code': 'NumpadAdd', 'location': 3}, + '-': {'keyCode': 109, 'key': '-', 'code': 'NumpadSubtract', 'location': 3}, + '/': {'keyCode': 111, 'key': '/', 'code': 'NumpadDivide', 'location': 3}, + ';': {'keyCode': 186, 'key': ';', 'code': 'Semicolon'}, + '=': {'keyCode': 187, 'key': '=', 'code': 'Equal'}, + ',': {'keyCode': 188, 'key': ',', 'code': 'Comma'}, + '.': {'keyCode': 190, 'key': '.', 'code': 'Period'}, + '`': {'keyCode': 192, 'key': '`', 'code': 'Backquote'}, + '[': {'keyCode': 219, 'key': '[', 'code': 'BracketLeft'}, + '\\': {'keyCode': 220, 'key': '\\', 'code': 'Backslash'}, + ']': {'keyCode': 221, 'key': ']', 'code': 'BracketRight'}, + '\'': {'keyCode': 222, 'key': '\'', 'code': 'Quote'}, + ')': {'keyCode': 48, 'key': ')', 'code': 'Digit0'}, + '!': {'keyCode': 49, 'key': '!', 'code': 'Digit1'}, + '@': {'keyCode': 50, 'key': '@', 'code': 'Digit2'}, + '#': {'keyCode': 51, 'key': '#', 'code': 'Digit3'}, + '$': {'keyCode': 52, 'key': '$', 'code': 'Digit4'}, + '%': {'keyCode': 53, 'key': '%', 'code': 'Digit5'}, + '^': {'keyCode': 54, 'key': '^', 'code': 'Digit6'}, + '&': {'keyCode': 55, 'key': '&', 'code': 'Digit7'}, + '(': {'keyCode': 57, 'key': '(', 'code': 'Digit9'}, + 'A': {'keyCode': 65, 'key': 'A', 'code': 'KeyA'}, + 'B': {'keyCode': 66, 'key': 'B', 'code': 'KeyB'}, + 'C': {'keyCode': 67, 'key': 'C', 'code': 'KeyC'}, + 'D': {'keyCode': 68, 'key': 'D', 'code': 'KeyD'}, + 'E': {'keyCode': 69, 'key': 'E', 'code': 'KeyE'}, + 'F': {'keyCode': 70, 'key': 'F', 'code': 'KeyF'}, + 'G': {'keyCode': 71, 'key': 'G', 'code': 'KeyG'}, + 'H': {'keyCode': 72, 'key': 'H', 'code': 'KeyH'}, + 'I': {'keyCode': 73, 'key': 'I', 'code': 'KeyI'}, + 'J': {'keyCode': 74, 'key': 'J', 'code': 'KeyJ'}, + 'K': {'keyCode': 75, 'key': 'K', 'code': 'KeyK'}, + 'L': {'keyCode': 76, 'key': 'L', 'code': 'KeyL'}, + 'M': {'keyCode': 77, 'key': 'M', 'code': 'KeyM'}, + 'N': {'keyCode': 78, 'key': 'N', 'code': 'KeyN'}, + 'O': {'keyCode': 79, 'key': 'O', 'code': 'KeyO'}, + 'P': {'keyCode': 80, 'key': 'P', 'code': 'KeyP'}, + 'Q': {'keyCode': 81, 'key': 'Q', 'code': 'KeyQ'}, + 'R': {'keyCode': 82, 'key': 'R', 'code': 'KeyR'}, + 'S': {'keyCode': 83, 'key': 'S', 'code': 'KeyS'}, + 'T': {'keyCode': 84, 'key': 'T', 'code': 'KeyT'}, + 'U': {'keyCode': 85, 'key': 'U', 'code': 'KeyU'}, + 'V': {'keyCode': 86, 'key': 'V', 'code': 'KeyV'}, + 'W': {'keyCode': 87, 'key': 'W', 'code': 'KeyW'}, + 'X': {'keyCode': 88, 'key': 'X', 'code': 'KeyX'}, + 'Y': {'keyCode': 89, 'key': 'Y', 'code': 'KeyY'}, + 'Z': {'keyCode': 90, 'key': 'Z', 'code': 'KeyZ'}, + ':': {'keyCode': 186, 'key': ':', 'code': 'Semicolon'}, + '<': {'keyCode': 188, 'key': '<', 'code': 'Comma'}, + '_': {'keyCode': 189, 'key': '_', 'code': 'Minus'}, + '>': {'keyCode': 190, 'key': '>', 'code': 'Period'}, + '?': {'keyCode': 191, 'key': '?', 'code': 'Slash'}, + '~': {'keyCode': 192, 'key': '~', 'code': 'Backquote'}, + '{': {'keyCode': 219, 'key': '{', 'code': 'BracketLeft'}, + '|': {'keyCode': 220, 'key': '|', 'code': 'Backslash'}, + # '\ue026': {'keyCode': 220, 'key': '|', 'code': 'Backslash'}, + '}': {'keyCode': 221, 'key': '}', 'code': 'BracketRight'}, + '"': {'keyCode': 222, 'key': '"', 'code': 'Quote'}, '\n': {'keyCode': 13, 'code': 'Enter', 'key': 'Enter', 'text': '\r'}, + '\ue007': {'keyCode': 13, 'code': 'Enter', 'key': 'Enter', 'text': '\r'}, + '\ue003': {'keyCode': 8, 'code': 'Backspace', 'key': 'Backspace'}, + '\ue00d': {'keyCode': 32, 'code': 'Space', 'key': ' '}, + # 'PageUp': {'keyCode': 33, 'shiftKeyCode': 105, 'key': 'PageUp', 'code': 'Numpad9', 'shiftKey': '9', 'location': 3}, + '\ue00e': {'keyCode': 33, 'code': 'PageUp', 'key': 'PageUp'}, + # 'PageDown': {'keyCode': 34, 'shiftKeyCode': 99, 'key': 'PageDown', 'code': 'Numpad3', 'shiftKey': '3', 'location': 3}, + '\ue00f': {'keyCode': 34, 'code': 'PageDown', 'key': 'PageDown'}, '\ue008': {'keyCode': 16, 'code': 'ShiftLeft', 'key': 'Shift', 'location': 1}, # 'ShiftRight': {'keyCode': 16, 'code': 'ShiftRight', 'key': 'Shift', 'location': 2}, '\ue009': {'keyCode': 17, 'code': 'ControlLeft', 'key': 'Control', 'location': 1}, # 'ControlRight': {'keyCode': 17, 'code': 'ControlRight', 'key': 'Control', 'location': 2}, '\ue00a': {'keyCode': 18, 'code': 'AltLeft', 'key': 'Alt', 'location': 1}, # 'AltRight': {'keyCode': 18, 'code': 'AltRight', 'key': 'Alt', 'location': 2}, - '\ue00b': {'keyCode': 19, 'code': 'Pause', 'key': 'Pause'}, - 'CapsLock': {'keyCode': 20, 'code': 'CapsLock', 'key': 'CapsLock'}, - '\ue00c': {'keyCode': 27, 'code': 'Escape', 'key': 'Escape'}, - 'Convert': {'keyCode': 28, 'code': 'Convert', 'key': 'Convert'}, - 'NonConvert': {'keyCode': 29, 'code': 'NonConvert', 'key': 'NonConvert'}, - '\ue00d': {'keyCode': 32, 'code': 'Space', 'key': ' '}, - # 'PageUp': {'keyCode': 33, 'shiftKeyCode': 105, 'key': 'PageUp', 'code': 'Numpad9', 'shiftKey': '9', 'location': 3}, - '\ue00e': {'keyCode': 33, 'code': 'PageUp', 'key': 'PageUp'}, - # 'PageDown': {'keyCode': 34, 'shiftKeyCode': 99, 'key': 'PageDown', 'code': 'Numpad3', 'shiftKey': '3', 'location': 3}, - '\ue00f': {'keyCode': 34, 'code': 'PageDown', 'key': 'PageDown'}, - '\ue010': {'keyCode': 35, 'code': 'End', 'key': 'End'}, - # 'Numpad1': {'keyCode': 35, 'shiftKeyCode': 97, 'key': 'End', 'code': 'Numpad1', 'shiftKey': '1', 'location': 3}, + '\ue03d': {'keyCode': 91, 'key': 'Meta', 'code': 'MetaLeft'}, + '\r': {'keyCode': 13, 'code': 'Enter', 'key': 'Enter', 'text': '\r'}, '\ue011': {'keyCode': 36, 'code': 'Home', 'key': 'Home'}, # 'Numpad7': {'keyCode': 36, 'shiftKeyCode': 103, 'key': 'Home', 'code': 'Numpad7', 'shiftKey': '7', 'location': 3}, '\ue012': {'keyCode': 37, 'code': 'ArrowLeft', 'key': 'ArrowLeft'}, @@ -133,6 +212,19 @@ keyDefinitions = { # 'Numpad6': {'keyCode': 39, 'shiftKeyCode': 102, 'key': 'ArrowRight', 'code': 'Numpad6', 'shiftKey': '6', 'location': 3}, # 'Numpad2': {'keyCode': 40, 'shiftKeyCode': 98, 'key': 'ArrowDown', 'code': 'Numpad2', 'shiftKey': '2', 'location': 3}, '\ue015': {'keyCode': 40, 'code': 'ArrowDown', 'key': 'ArrowDown'}, + + '\ue001': {'keyCode': 3, 'code': 'Abort', 'key': 'Cancel'}, + '\ue002': {'keyCode': 6, 'code': 'Help', 'key': 'Help'}, + '\ue004': {'keyCode': 9, 'code': 'Tab', 'key': 'Tab'}, + '\ue005': {'keyCode': 12, 'shiftKeyCode': 101, 'key': 'Clear', 'code': 'Numpad5', 'shiftKey': '5', 'location': 3}, + '\ue006': {'keyCode': 13, 'code': 'NumpadEnter', 'key': 'Enter', 'text': '\r', 'location': 3}, + '\ue00b': {'keyCode': 19, 'code': 'Pause', 'key': 'Pause'}, + 'CapsLock': {'keyCode': 20, 'code': 'CapsLock', 'key': 'CapsLock'}, + '\ue00c': {'keyCode': 27, 'code': 'Escape', 'key': 'Escape'}, + 'Convert': {'keyCode': 28, 'code': 'Convert', 'key': 'Convert'}, + 'NonConvert': {'keyCode': 29, 'code': 'NonConvert', 'key': 'NonConvert'}, + '\ue010': {'keyCode': 35, 'code': 'End', 'key': 'End'}, + # 'Numpad1': {'keyCode': 35, 'shiftKeyCode': 97, 'key': 'End', 'code': 'Numpad1', 'shiftKey': '1', 'location': 3}, 'Select': {'keyCode': 41, 'code': 'Select', 'key': 'Select'}, 'Open': {'keyCode': 43, 'code': 'Open', 'key': 'Execute'}, 'PrintScreen': {'keyCode': 44, 'code': 'PrintScreen', 'key': 'PrintScreen'}, @@ -238,102 +330,17 @@ keyDefinitions = { 'Alt': {'keyCode': 18, 'key': 'Alt', 'code': 'AltLeft'}, 'Accept': {'keyCode': 30, 'key': 'Accept'}, 'ModeChange': {'keyCode': 31, 'key': 'ModeChange'}, - ' ': {'keyCode': 32, 'key': ' ', 'code': 'Space'}, 'Print': {'keyCode': 42, 'key': 'Print'}, 'Execute': {'keyCode': 43, 'key': 'Execute', 'code': 'Open'}, '\u0000': {'keyCode': 46, 'key': '\u0000', 'code': 'NumpadDecimal', 'location': 3}, - 'a': {'keyCode': 65, 'key': 'a', 'code': 'KeyA'}, - 'b': {'keyCode': 66, 'key': 'b', 'code': 'KeyB'}, - 'c': {'keyCode': 67, 'key': 'c', 'code': 'KeyC'}, - 'd': {'keyCode': 68, 'key': 'd', 'code': 'KeyD'}, - 'e': {'keyCode': 69, 'key': 'e', 'code': 'KeyE'}, - 'f': {'keyCode': 70, 'key': 'f', 'code': 'KeyF'}, - 'g': {'keyCode': 71, 'key': 'g', 'code': 'KeyG'}, - 'h': {'keyCode': 72, 'key': 'h', 'code': 'KeyH'}, - 'i': {'keyCode': 73, 'key': 'i', 'code': 'KeyI'}, - 'j': {'keyCode': 74, 'key': 'j', 'code': 'KeyJ'}, - 'k': {'keyCode': 75, 'key': 'k', 'code': 'KeyK'}, - 'l': {'keyCode': 76, 'key': 'l', 'code': 'KeyL'}, - 'm': {'keyCode': 77, 'key': 'm', 'code': 'KeyM'}, - 'n': {'keyCode': 78, 'key': 'n', 'code': 'KeyN'}, - 'o': {'keyCode': 79, 'key': 'o', 'code': 'KeyO'}, - 'p': {'keyCode': 80, 'key': 'p', 'code': 'KeyP'}, - 'q': {'keyCode': 81, 'key': 'q', 'code': 'KeyQ'}, - 'r': {'keyCode': 82, 'key': 'r', 'code': 'KeyR'}, - 's': {'keyCode': 83, 'key': 's', 'code': 'KeyS'}, - 't': {'keyCode': 84, 'key': 't', 'code': 'KeyT'}, - 'u': {'keyCode': 85, 'key': 'u', 'code': 'KeyU'}, - 'v': {'keyCode': 86, 'key': 'v', 'code': 'KeyV'}, - 'w': {'keyCode': 87, 'key': 'w', 'code': 'KeyW'}, - 'x': {'keyCode': 88, 'key': 'x', 'code': 'KeyX'}, - 'y': {'keyCode': 89, 'key': 'y', 'code': 'KeyY'}, - 'z': {'keyCode': 90, 'key': 'z', 'code': 'KeyZ'}, - '\ue03d': {'keyCode': 91, 'key': 'Meta', 'code': 'MetaLeft'}, - '*': {'keyCode': 106, 'key': '*', 'code': 'NumpadMultiply', 'location': 3}, - '+': {'keyCode': 107, 'key': '+', 'code': 'NumpadAdd', 'location': 3}, - '-': {'keyCode': 109, 'key': '-', 'code': 'NumpadSubtract', 'location': 3}, - '/': {'keyCode': 111, 'key': '/', 'code': 'NumpadDivide', 'location': 3}, - ';': {'keyCode': 186, 'key': ';', 'code': 'Semicolon'}, - '=': {'keyCode': 187, 'key': '=', 'code': 'Equal'}, - ',': {'keyCode': 188, 'key': ',', 'code': 'Comma'}, - '.': {'keyCode': 190, 'key': '.', 'code': 'Period'}, - '`': {'keyCode': 192, 'key': '`', 'code': 'Backquote'}, - '[': {'keyCode': 219, 'key': '[', 'code': 'BracketLeft'}, - '\\': {'keyCode': 220, 'key': '\\', 'code': 'Backslash'}, - ']': {'keyCode': 221, 'key': ']', 'code': 'BracketRight'}, - '\'': {'keyCode': 222, 'key': '\'', 'code': 'Quote'}, 'Attn': {'keyCode': 246, 'key': 'Attn'}, 'CrSel': {'keyCode': 247, 'key': 'CrSel', 'code': 'Props'}, 'ExSel': {'keyCode': 248, 'key': 'ExSel'}, 'EraseEof': {'keyCode': 249, 'key': 'EraseEof'}, 'Play': {'keyCode': 250, 'key': 'Play'}, 'ZoomOut': {'keyCode': 251, 'key': 'ZoomOut'}, - ')': {'keyCode': 48, 'key': ')', 'code': 'Digit0'}, - '!': {'keyCode': 49, 'key': '!', 'code': 'Digit1'}, - '@': {'keyCode': 50, 'key': '@', 'code': 'Digit2'}, - '#': {'keyCode': 51, 'key': '#', 'code': 'Digit3'}, - '$': {'keyCode': 52, 'key': '$', 'code': 'Digit4'}, - '%': {'keyCode': 53, 'key': '%', 'code': 'Digit5'}, - '^': {'keyCode': 54, 'key': '^', 'code': 'Digit6'}, - '&': {'keyCode': 55, 'key': '&', 'code': 'Digit7'}, - '(': {'keyCode': 57, 'key': '(', 'code': 'Digit9'}, - 'A': {'keyCode': 65, 'key': 'A', 'code': 'KeyA'}, - 'B': {'keyCode': 66, 'key': 'B', 'code': 'KeyB'}, - 'C': {'keyCode': 67, 'key': 'C', 'code': 'KeyC'}, - 'D': {'keyCode': 68, 'key': 'D', 'code': 'KeyD'}, - 'E': {'keyCode': 69, 'key': 'E', 'code': 'KeyE'}, - 'F': {'keyCode': 70, 'key': 'F', 'code': 'KeyF'}, - 'G': {'keyCode': 71, 'key': 'G', 'code': 'KeyG'}, - 'H': {'keyCode': 72, 'key': 'H', 'code': 'KeyH'}, - 'I': {'keyCode': 73, 'key': 'I', 'code': 'KeyI'}, - 'J': {'keyCode': 74, 'key': 'J', 'code': 'KeyJ'}, - 'K': {'keyCode': 75, 'key': 'K', 'code': 'KeyK'}, - 'L': {'keyCode': 76, 'key': 'L', 'code': 'KeyL'}, - 'M': {'keyCode': 77, 'key': 'M', 'code': 'KeyM'}, - 'N': {'keyCode': 78, 'key': 'N', 'code': 'KeyN'}, - 'O': {'keyCode': 79, 'key': 'O', 'code': 'KeyO'}, - 'P': {'keyCode': 80, 'key': 'P', 'code': 'KeyP'}, - 'Q': {'keyCode': 81, 'key': 'Q', 'code': 'KeyQ'}, - 'R': {'keyCode': 82, 'key': 'R', 'code': 'KeyR'}, - 'S': {'keyCode': 83, 'key': 'S', 'code': 'KeyS'}, - 'T': {'keyCode': 84, 'key': 'T', 'code': 'KeyT'}, - 'U': {'keyCode': 85, 'key': 'U', 'code': 'KeyU'}, - 'V': {'keyCode': 86, 'key': 'V', 'code': 'KeyV'}, - 'W': {'keyCode': 87, 'key': 'W', 'code': 'KeyW'}, - 'X': {'keyCode': 88, 'key': 'X', 'code': 'KeyX'}, - 'Y': {'keyCode': 89, 'key': 'Y', 'code': 'KeyY'}, - 'Z': {'keyCode': 90, 'key': 'Z', 'code': 'KeyZ'}, - ':': {'keyCode': 186, 'key': ':', 'code': 'Semicolon'}, - '<': {'keyCode': 188, 'key': '<', 'code': 'Comma'}, - '_': {'keyCode': 189, 'key': '_', 'code': 'Minus'}, - '>': {'keyCode': 190, 'key': '>', 'code': 'Period'}, - '?': {'keyCode': 191, 'key': '?', 'code': 'Slash'}, - '~': {'keyCode': 192, 'key': '~', 'code': 'Backquote'}, - '{': {'keyCode': 219, 'key': '{', 'code': 'BracketLeft'}, - '|': {'keyCode': 220, 'key': '|', 'code': 'Backslash'}, - # '\ue026': {'keyCode': 220, 'key': '|', 'code': 'Backslash'}, - '}': {'keyCode': 221, 'key': '}', 'code': 'BracketRight'}, - '"': {'keyCode': 222, 'key': '"', 'code': 'Quote'}, + 'Power': {'key': 'Power', 'code': 'Power'}, + 'Eject': {'key': 'Eject', 'code': 'Eject'}, } modifierBit = {'\ue00a': 1, '\ue009': 2, @@ -405,10 +412,7 @@ def keyDescriptionForString(_modifiers, keyString): # noqa: C901 def send_key(page, modifier, key): """发送一个字,在键盘中的字符触发按键,其它直接发送文本""" - if key not in keyDefinitions: - page.run_cdp('Input.insertText', text=key, _ignore=AlertExistsError) - - else: + if key in keyDefinitions: description = keyDescriptionForString(modifier, key) text = description['text'] data = {'type': 'keyDown' if text else 'rawKeyDown', @@ -427,6 +431,9 @@ def send_key(page, modifier, key): data['type'] = 'keyUp' page.run_cdp('Input.dispatchKeyEvent', **data) + else: + page.run_cdp('Input.insertText', text=key, _ignore=AlertExistsError) + def input_text_or_keys(page, text_or_keys): """输入文本,也可输入组合键,组合键用tuple形式输入 diff --git a/DrissionPage/_functions/keys.pyi b/DrissionPage/_functions/keys.pyi index 896bc42..a06c8be 100644 --- a/DrissionPage/_functions/keys.pyi +++ b/DrissionPage/_functions/keys.pyi @@ -12,6 +12,12 @@ from .._pages.chromium_base import ChromiumBase class Keys: """特殊按键""" + CTRL_A: tuple + CTRL_C: tuple + CTRL_X: tuple + CTRL_V: tuple + CTRL_Z: tuple + CTRL_Y: tuple NULL: str CANCEL: str diff --git a/DrissionPage/_pages/chromium_page.py b/DrissionPage/_pages/chromium_page.py index fc18ff0..f2b0fe1 100644 --- a/DrissionPage/_pages/chromium_page.py +++ b/DrissionPage/_pages/chromium_page.py @@ -156,7 +156,7 @@ class ChromiumPage(ChromiumBase): """ return get_pdf(self, path, name, kwargs) if as_pdf else get_mhtml(self, path, name) - def get_tab(self, id_or_num=None, title=None, url=None, tab_type=None, as_id=False): + def get_tab(self, id_or_num=None, title=None, url=None, tab_type='page', as_id=False): """获取一个标签页对象,id_or_num不为None时,后面几个参数无效 :param id_or_num: 要获取的标签页id或序号,序号从1开始,可传入负数获取倒数第几个,不是视觉排列顺序,而是激活顺序 :param title: 要匹配title的文本,模糊匹配,为None则匹配所有 @@ -194,7 +194,7 @@ class ChromiumPage(ChromiumBase): with self._lock: return ChromiumTab(self, id_or_num) - def get_tabs(self, title=None, url=None, tab_type=None, as_id=False): + def get_tabs(self, title=None, url=None, tab_type='page', as_id=False): """查找符合条件的tab,返回它们组成的列表 :param title: 要匹配title的文本,模糊匹配,为None则匹配所有 :param url: 要匹配url的文本,模糊匹配,为None则匹配所有 diff --git a/DrissionPage/_pages/chromium_page.pyi b/DrissionPage/_pages/chromium_page.pyi index 2b7115f..7117b9e 100644 --- a/DrissionPage/_pages/chromium_page.pyi +++ b/DrissionPage/_pages/chromium_page.pyi @@ -94,13 +94,13 @@ class ChromiumPage(ChromiumBase): id_or_num: Union[str, ChromiumTab, int] = None, title: str = None, url: str = None, - tab_type: Union[str, list, tuple] = None, + tab_type: Union[str, list, tuple] = 'page', as_id: bool = False) -> Union[ChromiumTab, str, None]: ... def get_tabs(self, title: str = None, url: str = None, - tab_type: Union[str, list, tuple] = None, + tab_type: Union[str, list, tuple] = 'page', as_id: bool = False) -> Union[List[ChromiumTab], List[str]]: ... def new_tab(self, url: str = None, new_window: bool = False, background: bool = False, diff --git a/DrissionPage/_pages/web_page.py b/DrissionPage/_pages/web_page.py index 969b0cf..3a35e18 100644 --- a/DrissionPage/_pages/web_page.py +++ b/DrissionPage/_pages/web_page.py @@ -308,7 +308,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): elif self._mode == 'd': return super(SessionPage, self).cookies(as_dict, all_domains, all_info) - def get_tab(self, id_or_num=None, title=None, url=None, tab_type=None, as_id=False): + def get_tab(self, id_or_num=None, title=None, url=None, tab_type='page', as_id=False): """获取一个标签页对象,id_or_num不为None时,后面几个参数无效 :param id_or_num: 要获取的标签页id或序号,序号从1开始,可传入负数获取倒数第几个,不是视觉排列顺序,而是激活顺序 :param title: 要匹配title的文本,模糊匹配,为None则匹配所有 @@ -341,7 +341,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): with self._lock: return WebPageTab(self, id_or_num) - def get_tabs(self, title=None, url=None, tab_type=None, as_id=False): + def get_tabs(self, title=None, url=None, tab_type='page', as_id=False): """查找符合条件的tab,返回它们组成的列表 :param title: 要匹配title的文本,模糊匹配,为None则匹配所有 :param url: 要匹配url的文本,模糊匹配,为None则匹配所有 diff --git a/DrissionPage/_pages/web_page.pyi b/DrissionPage/_pages/web_page.pyi index bca5147..bc0be0b 100644 --- a/DrissionPage/_pages/web_page.pyi +++ b/DrissionPage/_pages/web_page.pyi @@ -131,13 +131,13 @@ class WebPage(SessionPage, ChromiumPage, BasePage): id_or_num: Union[str, WebPageTab, int] = None, title: str = None, url: str = None, - tab_type: Union[str, list, tuple] = None, + tab_type: Union[str, list, tuple] = 'page', as_id: bool = False) -> Union[WebPageTab, str, None]: ... def get_tabs(self, title: str = None, url: str = None, - tab_type: Union[str, list, tuple] = None, + tab_type: Union[str, list, tuple] = 'page', as_id: bool = False) -> Union[List[WebPageTab], List[str]]: ... def new_tab(self, diff --git a/DrissionPage/_units/actions.py b/DrissionPage/_units/actions.py index 171a68f..d119629 100644 --- a/DrissionPage/_units/actions.py +++ b/DrissionPage/_units/actions.py @@ -8,7 +8,7 @@ from time import sleep, perf_counter from ..errors import AlertExistsError -from .._functions.keys import modifierBit, keyDescriptionForString, input_text_or_keys, Keys +from .._functions.keys import modifierBit, keyDescriptionForString, input_text_or_keys, Keys, keyDefinitions from .._functions.web import location_in_viewport @@ -274,18 +274,23 @@ class Actions: return self def type(self, keys): - """用模拟键盘按键方式输入文本,可输入字符串,也可输入组合键,只能输入键盘上有的字符 + """用模拟键盘按键方式输入文本,可输入字符串,也可输入组合键 :param keys: 要按下的按键,特殊字符和多个文本可用list或tuple传入 :return: self """ modifiers = [] for i in keys: for character in i: - self.key_down(character) - if character in ('\ue009', '\ue008', '\ue00a', '\ue03d'): - modifiers.append(character) + if character in keyDefinitions: + self.key_down(character) + if character in ('\ue009', '\ue008', '\ue00a', '\ue03d'): + modifiers.append(character) + else: + self.key_up(character) + else: - self.key_up(character) + self.owner.run_cdp('Input.dispatchKeyEvent', type='char', text=character) + for m in modifiers: self.key_up(m) return self