|
18 | 18 | """The WebDriver implementation.""" |
19 | 19 | import contextlib |
20 | 20 | import copy |
| 21 | +import pkgutil |
21 | 22 | import types |
22 | 23 | import typing |
23 | | -from importlib import import_module |
24 | | - |
25 | | -import pkgutil |
26 | | - |
27 | | -from typing import Dict, List, Optional, Union |
28 | | - |
29 | 24 | import warnings |
30 | | - |
31 | 25 | from abc import ABCMeta |
32 | 26 | from base64 import b64decode, urlsafe_b64encode |
33 | 27 | from contextlib import asynccontextmanager, contextmanager |
34 | | - |
35 | | -from .bidi_connection import BidiConnection |
36 | | -from .command import Command |
37 | | -from .errorhandler import ErrorHandler |
38 | | -from .file_detector import FileDetector, LocalFileDetector |
39 | | -from .mobile import Mobile |
40 | | -from .remote_connection import RemoteConnection |
41 | | -from .script_key import ScriptKey |
42 | | -from .shadowroot import ShadowRoot |
43 | | -from .switch_to import SwitchTo |
44 | | -from .webelement import WebElement |
| 28 | +from importlib import import_module |
| 29 | +from typing import Dict, List, Optional, Union |
45 | 30 |
|
46 | 31 | from selenium.common.exceptions import (InvalidArgumentException, |
47 | 32 | JavascriptException, |
48 | 33 | WebDriverException, |
49 | 34 | NoSuchCookieException, |
50 | 35 | NoSuchElementException) |
51 | 36 | from selenium.webdriver.common.by import By |
| 37 | +from selenium.webdriver.common.html5.application_cache import ApplicationCache |
52 | 38 | from selenium.webdriver.common.options import BaseOptions |
53 | 39 | from selenium.webdriver.common.print_page_options import PrintOptions |
54 | 40 | from selenium.webdriver.common.timeouts import Timeouts |
55 | | -from selenium.webdriver.common.html5.application_cache import ApplicationCache |
56 | | -from selenium.webdriver.support.relative_locator import RelativeBy |
57 | 41 | from selenium.webdriver.common.virtual_authenticator import ( |
58 | 42 | Credential, |
59 | 43 | VirtualAuthenticatorOptions, |
60 | 44 | required_virtual_authenticator |
61 | 45 | ) |
62 | | - |
| 46 | +from selenium.webdriver.support.relative_locator import RelativeBy |
| 47 | +from .bidi_connection import BidiConnection |
| 48 | +from .command import Command |
| 49 | +from .errorhandler import ErrorHandler |
| 50 | +from .file_detector import FileDetector, LocalFileDetector |
| 51 | +from .mobile import Mobile |
| 52 | +from .remote_connection import RemoteConnection |
| 53 | +from .script_key import ScriptKey |
| 54 | +from .shadowroot import ShadowRoot |
| 55 | +from .switch_to import SwitchTo |
| 56 | +from .webelement import WebElement |
63 | 57 |
|
64 | 58 | _W3C_CAPABILITY_NAMES = frozenset([ |
65 | 59 | 'acceptInsecureCerts', |
@@ -456,16 +450,21 @@ def title(self) -> str: |
456 | 450 | """ |
457 | 451 | return self.execute(Command.GET_TITLE).get("value", "") |
458 | 452 |
|
459 | | - def pin_script(self, script, script_key=None) -> ScriptKey: |
460 | | - _script_key = ScriptKey(script_key) |
461 | | - self.pinned_scripts[_script_key.id] = script |
462 | | - return _script_key |
| 453 | + def pin_script(self, script: str, script_key=None) -> ScriptKey: |
| 454 | + """Store common javascript scripts to be executed later by a unique hashable ID.""" |
| 455 | + script_key_instance = ScriptKey(script_key) |
| 456 | + self.pinned_scripts[script_key_instance.id] = script |
| 457 | + return script_key_instance |
463 | 458 |
|
464 | | - def unpin(self, script_key) -> None: |
465 | | - self.pinned_scripts.pop(script_key.id) |
| 459 | + def unpin(self, script_key: ScriptKey) -> None: |
| 460 | + """Remove a pinned script from storage.""" |
| 461 | + try: |
| 462 | + self.pinned_scripts.pop(script_key.id) |
| 463 | + except KeyError: |
| 464 | + raise KeyError(f"No script with key: {script_key} existed in {self.pinned_scripts}") from None |
466 | 465 |
|
467 | 466 | def get_pinned_scripts(self) -> List[str]: |
468 | | - return list(self.pinned_scripts.keys()) |
| 467 | + return list(self.pinned_scripts) |
469 | 468 |
|
470 | 469 | def execute_script(self, script, *args): |
471 | 470 | """ |
|
0 commit comments