- 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
Running javascript in Selenium using Python.
We can run Javascript in Selenium webdriver with Python. The Document Object Model communicates with the elements on the page with the help of Javascript. Selenium executes the Javascript commands by taking the help of the execute_script method. The commands to be executed are passed as arguments to the method.
Some operations like scrolling down in a page cannot be performed by Selenium methods directly. This is achieved with the help of Javascript Executor. The window.scrollTo method is used to perform scrolling operation. The pixels to be scrolled horizontally along x axis and pixels to be scrolled vertically along y axis are passed as parameters to the method.
Syntax
driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
Example
Code Implementation to scroll till page bottom
from selenium import webdriver driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") driver.implicitly_wait(0.5) driver.get("https://www.tutorialspoint.com/tutor_connect/index.php") # to scroll till page bottom driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
We can also perform web operations like clicking on a link with Javascript Executor in Selenium. We shall use the execute_script method and pass argument index.click() and webelement to be clicked as arguments to the method.
Syntax
s = driver.find_element_by_css_selector("#id") driver.execute_script("arguments[0].click();",s)
Example
Code Implementation to perform web operations like click.
from selenium import webdriver driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") driver.implicitly_wait(0.5) driver.get("https://www.tutorialspoint.com/index.htm") # to identify element and then click s = driver.find_element_by_xpath("//*[text()='Library']") # perform click with execute_script method driver.execute_script("arguments[0].click();",s) print("Page title after click: " + driver.title)
Output
- Related Articles
- Running Selenium WebDriver python bindings in chrome.
- Running Selenium Webdriver with a proxy in Python.
- How to execute a Javascript using Selenium in Python?
- Running chrome browser in inconginto Mode in Selenium
- Using Extensions with Selenium & Python
- Reading JavaScript variables using Selenium WebDriver.
- How to close the pop up window in selenium running?
- Fill username and password using selenium in python.
- Fetch all href link using selenium in python.
- Get text using selenium web driver in python?
- Select iframe using Python and Selenium
- How to disable Javascript when using Selenium?
- How to execute a Javascript function in Python with Selenium?
- Accessing HTML source code using Python Selenium.
- How to Locate Elements using Selenium Python?
