- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to execute a Javascript function in Python with Selenium?
We can execute a JavaScript function in Python with Selenium webdriver. DOM interacts with the elements via JavaScript. Selenium is capable of executing JavaScript commands with the execute_script method.
Few actions like web scrolling cannot be done by Selenium directly. For this, we shall use the JavaScript Executor. We shall take the help of the JavaScript command window.scrollTo and pass it to the execute_script method. To scroll to the bottom of the page, we have to pass 0 and document.body.scrollHeight as parameters to the window.scrollTo.
Syntax
driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
Example
from selenium import webdriver driver = webdriver.Firefox(executable_path="C:\geckodriver.exe") driver.implicitly_wait(0.5) driver.get("https://www.tutorialspoint.com/index.htm") #scroll till page bottom driver.execute_script("window.scrollTo(0,document.body.scrollHeight);)
We can also do web action like click on a link with JavaScript. Here, also we shall utilize the execute_script method and pass arguments with index and element as parameters to that method.
Syntax
e = driver.find_element_by_css_selector(".cls") driver.execute_script("arguments[0].click();",e)
Let us click the link Library on the page.
Example
from selenium import webdriver driver = webdriver.Firefox(executable_path="C:\geckodriver.exe") driver.implicitly_wait(0.8) driver.get("https://www.tutorialspoint.com/index.htm") # to identify element l = driver.find_element_by_xpath("//*[text()='Library']") #click with execute_script driver.execute_script("arguments[0].click();",l) print("Page title after click: " + driver.title)
Output
- Related Articles
- How to execute a Javascript using Selenium in Python?
- How do you use Selenium to execute javascript within a frame?
- How to execute a JavaScript function using its name in a variable?
- How to click on a button with Javascript executor in Selenium with python?
- How to click on a link with a Javascript executor in Selenium with python?
- How to extract text from a Javascript alert in Selenium with python?
- How to perform vertical scrolling of a webpage with Javascript executor in Selenium with python?
- How to execute a JavaScript function when I have its name as a string?
- How to refresh a browser then navigate to a new page with Javascript executor in Selenium with python?
- How to get the inner text of a webpage with a Javascript executor in Selenium with python?
- How to execute a Python file in Python shell?
- How to get the title and URL of a webpage with Javascript executor in Selenium with python?
- How to invoke a function with a function constructor in JavaScript?
- How to key in values in input text box with Javascript executor in Selenium with python?
- How to use Selenium with Python?
