How to run JavaScript in Selenium Python, pass and get values from it

By Oleg Tolochko, published on 2023-12-22
How to run JavaScript in Selenium Python, pass and get values from it

Selenium is a popular library used for testing interfaces or creating bots for websites. Although most of the functionality is available in Python methods, sometimes we need to execute JavaScript code on a page. In this article, let's see how to do it.

To execute JavaScript in Selenium using Python, you can use the execute_script() method. This method allows you to execute JavaScript code in the context of the current page loaded in Selenium WebDriver. Here is a basic example that illustrates how it can be used:

from selenium import webdriver

# Initialize the WebDriver. In this example, I'm using ChromeDriver.
driver = webdriver.Chrome('/path/to/chromedriver')

# Open a webpage.
driver.get('http://example.com')

# Execute a JavaScript command.
driver.execute_script("alert('Hello, World!');")

# Don't forget to close the driver.
driver.quit()

Here, this execute_script() method is used to display a simple JavaScript alert(), but you can replace the JavaScript string with any script that needs to be executed.

Get data from JS in Python

To get data from the return statement of JavaScript in Selenium with Python, you can use the execute_script() method just like before, but this time fix its return value. The execute_script() method will return whatever the JavaScript code returns. Here's how you can do it:

from selenium import webdriver

# Initialize the WebDriver. Here I'm using ChromeDriver as an example.
driver = webdriver.Chrome('/path/to/chromedriver')

# Open a webpage.
driver.get('http://example.com')

# Execute a JavaScript command and capture the return value.
# For example, getting the title of the page using JavaScript.
title = driver.execute_script("return document.title;")

# Print the result.
print("The title of the page is:", title)

# Don't forget to close the driver.
driver.quit()

This example executes JavaScript return document.title;, which returns the title of the current page. This value is then stored to the Python variable title and printed.

You can replace "return document.title;" with any JavaScript code that returns data, and the method will return that data to your Python script.

Pass data to JavaScript

To pass arguments to the execute_script() method in Selenium using Python, you can include them in the method call after the JavaScript code line.

These arguments can then be retrieved in your JavaScript code. In JS, use the arguments[index] variable to reference the passed arguments, where index is the argument index (starting at 0).

from selenium import webdriver

# Initialize the WebDriver. Here I'm using ChromeDriver.
driver = webdriver.Chrome('/path/to/chromedriver')

# Open a webpage.
driver.get('http://example.com')

# JavaScript code with arguments
js_code = "return arguments[0] + arguments[1];"

# Pass arguments to execute_script
result = driver.execute_script(js_code, 10, 20)

# Output the result
print("The result is:", result)

# Close the driver
driver.quit()

In this example, the JavaScript code return arguments[0] + arguments[1]; sum two numbers. The numbers 10 and 20 are passed as arguments to execute_script(). The JavaScript code then returns the sum of these numbers, which is stored in the Python variable result and output to the console.

This can also be done by inserting a variable into the JS code line itself, but this sometimes causes problems with quotes, strings, so this option should be tested.

Written by Oleg Tolochko
Owner, author and editor of the ITWebMind.com

Since 2021 I have been working on freelancing, mainly in the field of advertising, development and promotion of websites. I am interested in programming, in particular Python, PHP, JS to a greater extent for myself and automation of my projects. Also design, SEO, context and targeted advertising, productivity, various areas of management. I am fond of playing guitar, horses, snorkeling. Traveling in Asia at the moment. In this blog I collect my work experience for myself and others, as well as improve my skills in design, website development and promotion, copywriting.

Please rate this article
(5 stars / 2 votes)