Attempt to load the current latest available devtools into the module cache for use later.
(ver)
| 53 | |
| 54 | |
| 55 | def import_devtools(ver): |
| 56 | """Attempt to load the current latest available devtools into the module cache for use later.""" |
| 57 | global devtools |
| 58 | global version |
| 59 | version = ver |
| 60 | base = "selenium.webdriver.common.devtools.v" |
| 61 | try: |
| 62 | devtools = importlib.import_module(f"{base}{ver}") |
| 63 | return devtools |
| 64 | except ModuleNotFoundError: |
| 65 | # Attempt to parse and load the 'most recent' devtools module. This is likely |
| 66 | # because cdp has been updated but selenium python has not been released yet. |
| 67 | devtools_path = pathlib.Path(__file__).parents[1].joinpath("devtools") |
| 68 | versions = tuple(f.name for f in devtools_path.iterdir() if f.is_dir()) |
| 69 | available_versions = tuple(x for x in versions if x == "latest" or (x.startswith("v") and x[1:].isdigit())) |
| 70 | numeric_versions = tuple(x[1:] for x in available_versions if x.startswith("v")) |
| 71 | if not numeric_versions: |
| 72 | raise |
| 73 | latest = max(numeric_versions, key=int) |
| 74 | selenium_logger = logging.getLogger(__name__) |
| 75 | selenium_logger.debug("Falling back to loading `devtools`: v%s", latest) |
| 76 | devtools = importlib.import_module(f"{base}{latest}") |
| 77 | return devtools |
| 78 | |
| 79 | |
| 80 | _connection_context: contextvars.ContextVar = contextvars.ContextVar("connection_context") |