How to Execute JavaScript#
When automating the execution of JavaScript, this can either be done with or without a WebElement
.
Without WebElement
#
Basic usage of JavaScript execution:
Python
from browserist import Browser
with Browser() as browser:
browser.open.url("https://example.com")
browser.tool.execute_script("alert('Hello world!')")
With WebElement
#
The WebElement
is used when you need to interact with a specific element on the page:
Python | |
---|---|
From a File#
Sometimes you want to run a script that is stored in a file. This can be done by reading the file before running the script: