javascript.py 968 B

12345678910111213141516171819202122
  1. from ..awaitable_response import AwaitableResponse
  2. from ..context import context
  3. def run_javascript(code: str, *, timeout: float = 1.0) -> AwaitableResponse:
  4. """Run JavaScript
  5. This function runs arbitrary JavaScript code on a page that is executed in the browser.
  6. The client must be connected before this function is called.
  7. To access a client-side object by ID, use the JavaScript function `getElement()`.
  8. If the function is awaited, the result of the JavaScript code is returned.
  9. Otherwise, the JavaScript code is executed without waiting for a response.
  10. Note that requesting data from the client is only supported for page functions, not for the shared auto-index page.
  11. :param code: JavaScript code to run
  12. :param timeout: timeout in seconds (default: `1.0`)
  13. :return: AwaitableResponse that can be awaited to get the result of the JavaScript code
  14. """
  15. return context.client.run_javascript(code, timeout=timeout)