execute_script driver method - Selenium Python

Introduction:

In this tutorial, we are learning about the execute_script driver method in Selenium Python. The Selenium module is used for automatic testing using Python. Selenium Python bindings provide a simple API. Using Selenium WebDriver, this API is used to write functional or acceptance tests. A web page can be opened using Selenium Python, checkout - Using Get Method, we can navigate the links - Selenium Python.

It is being able to go to the right place. We want to interact with the page. More specifically, the HTML content inside the page. There are many ideas for finding content using Selenium, such as Locating Strategies. The object model uses JavaScript to communicate with the content of the page. Selenium runs JavaScript commands with the execute_script method. Commands to be executed can be passed as arguments to this method.

Some actions, such as navigating a page, cannot be performed directly through Selenium. This is done with the help of a JavaScript Executor. The Window.scrollTo method is used to perform scroll operations in the web pages. Scrolling the pixel horizontally along the x-axis and vertically scrolling the pixel along the y-axis becomes impossible for this method.

Selenium WebDriver provides various methods to control sessions (e.g., browser). Some examples of Selenium WebDriver are adding cookies, pressing the back button, navigating between tabs, etc. This tutorial can revolve around the executive_script driver in Selenium Python. In the current window/frame, the execute_script method executes the JavaScript synchronously. This is the biggest part of Selenium because JavaScript can do anything with the web, from accessing APIs by using the live code.

Syntax:

The syntax of the execute_script driver method in Selenium Python is given below -

Here,

script: The script represents the JavaScript that will be executed.

*args: The *args represents any applicable arguments for the given JavaScript.

Example:

Now we give an example of using the execute_script method as a driver method, which we have given below -

Discuss with the program code about the execute_script driver method in Selenium Python.

Here, we learn the program code of the execute_script method using a web driver in Selenium Python. Now, we visit the web page https://www.javatpoint.com/ and operate it on the driver object. So, now we give the program code using Python, which is given below -

Program Code 1:

Output:

Now, we compile the above code in Python, and after successful compilation, we run it. The browser generates the alert and says, "The alerts will come via selenium". The output is given below -

execute_script driver method - Selenium Python

Program Code 2:

Now, we give another example of the execute_script method using a web driver in Selenium Python. Here, code implementation performs web operations like clicking on the web page. The code is now given below -

Output:

Now, we compile the above code in Python, and after successful compilation, we run it. The output is given below -

/home/saby/PycharmProject/HelloWorld/venv/bin/Python
The page title after clicking the page: Tutorials, Free Online Tutorials
Process finished with exit code 0

Conclusion:

So, in this tutorial, we are learning about the execute_script driver method in Selenium Python. It is automatic testing using Python. This tutorial can revolve around the executive_script driver in Selenium Python. In the current window/frame, the execute_script method executes the JavaScript synchronously. Here, we learn the syntax of the execute_script driver method. We also learn the program code of the execute_script driver method in Selenium Python and share the outputs of the given code.






Latest Courses