javascript.py 1023 B

123456789101112131415161718192021222324
  1. from typing import Optional
  2. from .. import context
  3. from ..awaitable_response import AwaitableResponse
  4. def run_javascript(code: str, *,
  5. respond: Optional[bool] = None,
  6. timeout: float = 1.0, check_interval: float = 0.01) -> AwaitableResponse:
  7. """Run JavaScript
  8. This function runs arbitrary JavaScript code on a page that is executed in the browser.
  9. The client must be connected before this function is called.
  10. To access a client-side object by ID, use the JavaScript function `getElement()`.
  11. If the function is awaited, the result of the JavaScript code is returned.
  12. Otherwise, the JavaScript code is executed without waiting for a response.
  13. :param code: JavaScript code to run
  14. :param timeout: timeout in seconds (default: `1.0`)
  15. :return: AwaitableResponse that can be awaited to get the result of the JavaScript code
  16. """
  17. return context.get_client().run_javascript(code, respond=respond, timeout=timeout, check_interval=check_interval)