- Selenium Features
- Selenium Installation
- Selenium Locators
- XPath Fundamentals
- CSS Selectors Methods
- Different Browsers Execution
- find_element & find_elements
- Check Enabled Status
- Check Displayed Status
- Check Selected Status
- Selenium Waits
- Send_keys Method
- Click Method
- Get Text
- Get Attribute Value
- Get Current URL
- Forward, Back, Refresh
- Take Screenshot
- Handle Browser Tabs
- Handle iframe
- Mouse Hover
- Context-Click
- Drag & Drop
- Handle Alerts
- Handle Dropdown
- Execute Javascript
- Scroll To element
- Headless Mode Execution
- Chrome Options
- Keyboard Action
Selenium is one of the most powerful automation tools used for web scraping, browser automation, and testing. Often, there are cases where interacting with web elements using Selenium’s native methods may not be enough. This is where executing JavaScript in Python Selenium becomes crucial. JavaScript execution allows automation scripts to perform actions that are not directly supported by Selenium’s built-in methods.
In this article, we will explore how to execute JavaScript using Python Selenium WebDriver effectively. We will cover various use cases, such as scrolling the page, clicking hidden elements, extracting data, modifying elements, and handling AJAX requests.
How to Execute JavaScript in Selenium
Selenium provides a built-in method called execute_script()
to run JavaScript commands. The syntax is as follows:
driver.execute_script("JavaScript_Code")
1. Scrolling the Web Page
Sometimes, elements are not visible until you scroll down. You can use JavaScript to scroll to a specific position or until an element is in view:
from selenium import webdriver
# Initialize the driver
driver = webdriver.Chrome()
driver.get(""https://automationbysqatools.blogspot.com/2021/05/dummy-website.html)
# Scroll down by 1000 pixels
driver.execute_script("window.scrollBy(0,1000);")
Alternatively, scroll to an element:
element = driver.find_element("id", "element_id")
driver.execute_script("arguments[0].scrollIntoView();", element)
2. Clicking Hidden Elements
Some elements are hidden due to overlays or display properties. Use JavaScript to forcefully click them:
element = driver.find_element("id", "hidden_button")
driver.execute_script("arguments[0].click();", element)
3. Extracting Data Using JavaScript
If an element’s text is dynamically generated via JavaScript, traditional Selenium methods might fail. Instead, execute JavaScript to retrieve the content:
text = driver.execute_script("return document.getElementById('element_id').innerText;")
print(text)
4. Modifying Web Elements
You can also use JavaScript to modify the DOM elements dynamically:
driver.execute_script("document.getElementById('element_id').value='New Value';")
5. Handling AJAX Requests
Sometimes, elements take time to load due to AJAX. You can use JavaScript to check when all AJAX calls are complete:
driver.execute_script("return jQuery.active == 0;")
Executing JavaScript with Return Values
JavaScript executed in Selenium can also return values, making it useful for retrieving attributes, inner text, and other information.
page_title = driver.execute_script("return document.title;")
print("Page Title:", page_title)
Getting Element Attributes
element = driver.find_element("id", "element_id")nattribute = driver.execute_script("return arguments[0].getAttribute('href');", element)
print(attribute)
Common Use Cases of JavaScript Execution in Selenium
1. Handling Disabled Elements
Some elements are disabled by default. Use JavaScript to enable and interact with them:
driver.execute_script("document.getElementById('submit_button').removeAttribute('disabled');")
2. Setting Cookies
To set cookies manually:
driver.execute_script("document.cookie = 'username=JohnDoe';")
3. Changing Element Style
Modify element styles dynamically:
driver.execute_script("document.getElementById('box').style.backgroundColor = 'red';")
Best Practices for Using JavaScript in Selenium
- Use JavaScript as a last resort – Always try Selenium’s native methods first.
- Ensure browser compatibility – Some JavaScript functions behave differently in various browsers.
- Handle exceptions properly – Wrap JavaScript execution in
try-except
blocks to catch errors. - Use explicit waits – JavaScript execution doesn’t wait for elements to load, so use
WebDriverWait
when necessary.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.ID, "element_id")))
driver.execute_script("arguments[0].click();", element)