Class for accessing logging APIs using the WebDriver Bidi protocol. This class is not to be used directly and should be used from the webdriver base classes.
| 35 | |
| 36 | |
| 37 | class Log: |
| 38 | """Class for accessing logging APIs using the WebDriver Bidi protocol. |
| 39 | |
| 40 | This class is not to be used directly and should be used from the |
| 41 | webdriver base classes. |
| 42 | """ |
| 43 | |
| 44 | def __init__(self, driver, bidi_session) -> None: |
| 45 | self.driver = driver |
| 46 | self.session = bidi_session.session |
| 47 | self.cdp = bidi_session.cdp |
| 48 | self.devtools = bidi_session.devtools |
| 49 | _pkg = ".".join(__name__.split(".")[:-1]) |
| 50 | # Ensure _mutation_listener_js is not None before decoding |
| 51 | _mutation_listener_js_bytes: bytes | None = pkgutil.get_data(_pkg, "mutation-listener.js") |
| 52 | if _mutation_listener_js_bytes is None: |
| 53 | raise ValueError("Failed to load mutation-listener.js") |
| 54 | self._mutation_listener_js = _mutation_listener_js_bytes.decode("utf8").strip() |
| 55 | |
| 56 | @asynccontextmanager |
| 57 | async def mutation_events(self) -> AsyncGenerator[dict[str, Any], None]: |
| 58 | """Listen for mutation events and emit them as they are found. |
| 59 | |
| 60 | .. deprecated:: |
| 61 | Use ``driver.script.add_dom_mutation_handler()`` instead, |
| 62 | which uses the WebDriver BiDi protocol. |
| 63 | |
| 64 | Example: |
| 65 | async with driver.log.mutation_events() as event: |
| 66 | pages.load("dynamic.html") |
| 67 | driver.find_element(By.ID, "reveal").click() |
| 68 | WebDriverWait(driver, 5)\ |
| 69 | .until(EC.visibility_of(driver.find_element(By.ID, "revealed"))) |
| 70 | |
| 71 | assert event["attribute_name"] == "style" |
| 72 | assert event["current_value"] == "" |
| 73 | assert event["old_value"] == "display:none;" |
| 74 | """ |
| 75 | warnings.warn( |
| 76 | "mutation_events is deprecated, use driver.script.add_dom_mutation_handler() instead", |
| 77 | DeprecationWarning, |
| 78 | stacklevel=2, |
| 79 | ) |
| 80 | page = self.cdp.get_session_context("page.enable") |
| 81 | await page.execute(self.devtools.page.enable()) |
| 82 | runtime = self.cdp.get_session_context("runtime.enable") |
| 83 | await runtime.execute(self.devtools.runtime.enable()) |
| 84 | await runtime.execute(self.devtools.runtime.add_binding("__webdriver_attribute")) |
| 85 | with warnings.catch_warnings(): |
| 86 | warnings.simplefilter("ignore", DeprecationWarning) |
| 87 | self.driver.pin_script(self._mutation_listener_js) |
| 88 | script_key = await page.execute( |
| 89 | self.devtools.page.add_script_to_evaluate_on_new_document(self._mutation_listener_js) |
| 90 | ) |
| 91 | with warnings.catch_warnings(): |
| 92 | warnings.simplefilter("ignore", DeprecationWarning) |
| 93 | self.driver.pin_script(self._mutation_listener_js, script_key) |
| 94 | self.driver.execute_script(f"return {self._mutation_listener_js}") |