DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Migrating from React Router v5 to v6: A Comprehensive Guide
  • React Callback Refs: What They Are and How to Use Them
  • Why React Router 7 Is a Game-Changer for React Developers
  • Functional Programming Principles Powering Python’s itertools Module

Trending

  • From Zero to Production: Best Practices for Scaling LLMs in the Enterprise
  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • Optimizing Integration Workflows With Spark Structured Streaming and Cloud Services
  • How To Develop a Truly Performant Mobile Application in 2025: A Case for Android
  1. DZone
  2. Coding
  3. Languages
  4. Perform Actions Using JavaScript in Python Selenium WebDriver

Perform Actions Using JavaScript in Python Selenium WebDriver

Let's take a look at a few different ways to execute JavaScript statements through Python Selenium WebDriver.

By 
Arunkumar Velusamy user avatar
Arunkumar Velusamy
·
Dec. 13, 18 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
171.5K Views

Join the DZone community and get the full member experience.

Join For Free

In this tutorial, let's analyze the least used but most powerful feature of Selenium WebDriver. Yes, I am going to discuss the JavaScript executor, and show you a few different ways to execute JavaScript statements through Python Selenium WebDriver.

It may so happen that in some real-time projects, Selenium WebDriver cannot perform an action on a particular web element. For example, since WebDriver simulates end-user interaction, it is natural that it will refuse to click on an element that is not visible to the end user (sometimes it also happens even though the web element is visible on the page). There can be several other similar reasons or scenarios.

In these cases, we can rely on JavaScript to click or perform actions on that web element, and these JavaScript statements can be executed through WebDriver.

You can do everything that the WebElement interface does and more with JavaScript.

What is JavaScript?

JavaScript is a scripting language that runs on client side, i.e, on the browser, and does some magic things when you surf through web pages. For more details, search for the keyword "JavaScript" on DZone.

How Do We Use JavaScript in WebDriver?

Python Selenium WebDriver provides a built-in method:

driver.execute_script("some javascript code here");


There are two ways we can execute JavaScript within the browser.

Method 1: Executing JavaScript at Document Root Level

In this case, we capture the element that we want to work with, using JavaScript-provided methods, then declare some actions on it and execute this JavaScript using WebDriver.

Example:

javaScript = "document.getElementsByName('username')[0].click();"
driver.execute_script(javaScript)


What Are We Doing Here?

Step 1: We are inspecting and fetching the element by a property 'name' using JavaScript. (Also, 'id' and 'class' properties can be used.)

Step 2: Declare and perform a click action on an element using JavaScript.

Step 3: Call execute_script() method and pass the JavaScript that we created as a string value.

Notice [0]   in the getElementsByName('username')[0]  statement above. JavaScript functions getElementsByName ,  getElementsByClassName , and so on return all matching elements as an array. In our case, we need to act on the first matching element that can be accessed via  index [0] . If you know what you are doing, i.e., if you know the index of the element you want to operate, you can directly use the index, such as  getElementsByName('username')[2] .

However, if you are using the JavaScript function ' getElementById ', you do not need to use any indexing, as it will return only one element (‘id’ should be unique).

While executing, WebDriver will inject the JavaScript statement into the browser and the script will perform the job. In our example, it performs a click operation on the target element. This JavaScript has its own namespace and does not interfere with the JavaScript in the actual web page.

Method 2: Executing JavaScript at Element Level

In this case, we capture the element that we want to work with using WebDriver, then we declare some actions on it using JavaScript and execute this JavaScript using WebDriver by passing the web element as an argument to the JavaScript.

Is this confusing? Let's break it down.

For example:

userName = driver.find_element_by_xpath("//button[@name='username']")
driver.execute_script("arguments[0].click();", userName)


What Are We Doing Here?

Step 1: Inspect and capture the element using WebDriver-provided methods like '  find_element_by_xpath ':

userName = driver.find_element_by_xpath("//button[@name='username']")


Step 2: Declare and perform a click action on the element using JavaScript:

arguments[0].click()


Step 3: Call execute_script()   method with the JavaScript statement that we created as a string value and web element captured using WebDriver as arguments:

driver.execute_script("arguments[0].click();", userName)


The above two lines of code can be shortened to the format below, where we find an element using WebDriver, declare some JavaScript functions, and execute the JavaScript using WebDriver.

driver.execute_script("arguments[0].click();", driver.find_element_by_xpath("//button[@name='username']"))


Another issue faced more frequently is the need to scroll to the bottom of the web page. You can perform this operation in a single line of code:

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")


Also, you can have more than one JavaScript action in your statement.

For example:

userName = driver.find_element_by_xpath("//button[@name='username']")
password = driver.find_element_by_xpath("//button[@name='password']")
driver.execute_script("arguments[0].click();arguments[1].click();", userName, password)


In this case, usage of the order of web elements matters. Accessing index  with [0]   anywhere inside a JavaScript statement will retrieve the first web element passed.

 driver.execute_script("arguments[1].click();arguments[0].click();", userName, password) 


How to Return Values

Another important aspect of the JavaScript executor is that it can be used to fetch values from web elements. That means the  execute_script()  method can return values.

For example:

print driver.execute_script('return document.getElementById("fsr").innerText')


Note that if you want something returned by JavaScript code, you need to use return. Also, elements can be located with Selenium and passed into the script.

What Happens When an Element Is Not Found?

When JavaScript cannot find an element to operate on, it throws a WebDriver exception with the respective error message.

Scenario 1: We're trying to read a property using ' print driver.execute_script('return document.getElementById("fsr").innerText') ' but there is no such element available in the web page. We get the following message in the exception trace:

selenium.common.exceptions.WebDriverException: Message: unknown error: Cannot read property 'innerText' of null


Scenario 2: We're trying to use an invalid operation or error function name inside JavaScript, like ' print driver.execute_script('document.getElementById("fsr").clic();') '. (Note the spelling mistake in the  click()  method name.)

selenium.common.exceptions.WebDriverException: Message: unknown error: document.getElementById(...).clic is not a function

Summary

Here is a summary of a few potential actions for which JavaScript could be used.

  • get elements text or attribute

  • find an element

  • do some operation on an element, like click()  

  • change attributes of an element

  • scroll to an element or location on a web page

  • wait until the page is loaded

Basic knowledge of JavaScript helps a lot when handling the DOM with Selenium. You can find more details and articles on the allselenium website.

JavaScript Element Python (language)

Published at DZone with permission of Arunkumar Velusamy. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Migrating from React Router v5 to v6: A Comprehensive Guide
  • React Callback Refs: What They Are and How to Use Them
  • Why React Router 7 Is a Game-Changer for React Developers
  • Functional Programming Principles Powering Python’s itertools Module

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!