Categories
Python Answers

How to run JavaScript in Selenium using Python?

Spread the love

Sometimes, we want to run JavaScript in Selenium using Python.

In this article, we’ll look at how to run JavaScript in Selenium using Python.

How to run JavaScript in Selenium using Python?

To run JavaScript in Selenium using Python, we can use the execute_script method.

For instance, we write

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://foo.com") 
driver.execute_script("document.getElementsByClassName('user')[0].click()")

to open the http://foo.com website with driver.get.

Then we call execute_script with the JavaScript code that we want to run in the string.

Conclusion

To run JavaScript in Selenium using Python, we can use the execute_script method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *