JavaScript Executor in Selenium Python
In the previous articles on Selenium Python Tutorial, we have covered “How to handle checkbox, static dropdowns and other UI elements in Selenium Python“. In this tutorial, we will learn JavaScript Executor in Selenium Python.
Document Object Model or DOM can access all the elements on the web page with the help of Javascript. If we inspect any element on a web page and navigate to the console in the Developer Tools, all the methods available to access the web elements shall be available.
Thus it has the capabilities that Selenium can perform. Selenium has the feature to execute Javascript code [to access the DOM] integrated with it. This is required whenever Selenium cannot identify an element or perform some actions on it.
Selenium contains the execute_script () method to execute Javascript commands which are passed as an argument to that method. Also, while working with DOM methods [getElementsByName and getElementsById and so on] fetch an array of matching elements. So we need to mention the index to locate a particular element. For example: getElementsByName (‘txt-box’) [0], refers to the first matching element on the web page.
Now let us see how to extract values from an edit box with the help of a Javascript executor.
# import the webdriver from selenium import webdriver driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/") # to enter text in edit box driver.find_element_by_name ("username").send_keys("Python") # extract the value with Javascript Executor print(driver.execute_script('return document.getElementsByName("username")[0].value'))
We can also perform click operations on web elements with the help of Javascript executors.
# import the webdriver from selenium import webdriver driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/") # to click on button with Javascript executor btn = driver.find_element_by_name ("spbutton").click() driver.execute_script ("arguments[0].click();",btn)
There are some operations like scrolling down a page that cannot be performed with the help of Selenium. For this action, Javascript Executor can be used for this purpose.
# import the webdriver from selenium import webdriver driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get ("https://www.softwaretestingmaterial.com/") # to scroll with Javascript executor driver.execute_script ("window.scrollTo(0,document.body.scrollHeight);")
We can also retrieve the current URL and title of the webpage with the help of Javascript Executor.
# import the webdriver from selenium import webdriver driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("https://www.softwaretestingmaterial.com/") # to scroll with Javascript executor driver.execute_script("window.scrollTo(0,document.body.scrollHeight);") # print the page title in console print(driver.execute_script('return document.title')) # print the page URL in console print(driver.execute_script('return document.URL')) # to close the browser driver.close()
In the next article, we will learn Synchronization in Selenium Python
Related posts:
- Selenium Python Tutorial
- Selenium Java Tutorial
- Selenium Interview Questions
- API Testing Tutorial
- Postman Tutorial