Sometimes, normal commands can't do everything on a webpage. JavaScript executor lets you run JavaScript code directly in the browser to control or check things.
JavaScript executor basics in Selenium Python
driver.execute_script("JavaScript code here")You use execute_script method of the Selenium WebDriver.
The JavaScript code is a string inside the parentheses and quotes.
driver.execute_script("alert('Hello from Selenium!')")driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")value.value = driver.execute_script("return document.title;")This script opens a webpage, scrolls to the bottom, prints the page title using JavaScript, changes the background color, and then closes the browser.
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By import time # Setup Chrome driver (adjust path as needed) service = Service('/path/to/chromedriver') driver = webdriver.Chrome(service=service) try: driver.get('https://example.com') # Scroll to bottom using JavaScript executor driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") time.sleep(2) # wait to see the scroll effect # Get page title using JavaScript title = driver.execute_script("return document.title;") print(f'Page title is: {title}') # Change background color using JavaScript driver.execute_script("document.body.style.backgroundColor = 'lightblue';") time.sleep(2) # wait to see the color change finally: driver.quit()
Always quit the driver to close the browser after tests.
Use return in JavaScript to get values back into Python.
JavaScript executor can run any JavaScript code, so be careful with syntax.
JavaScript executor runs JavaScript code inside the browser from Selenium.
It helps when normal Selenium commands can't interact with elements properly.
Use execute_script method with JavaScript code as a string.
Practice
execute_script in Selenium with Python?Solution
Step 1: Understand the role of
This method allows running JavaScript code inside the browser from Selenium tests.execute_scriptStep 2: Compare with other Selenium functions
Other functions locate elements or control the browser, butexecute_scriptspecifically runs JS code.Final Answer:
To run JavaScript code inside the browser during a test -> Option AQuick Check:
execute_script runs JS code [OK]
- Confusing execute_script with element locating methods
- Thinking execute_script starts WebDriver server
- Assuming execute_script takes screenshots
execute_script in Python?Solution
Step 1: Check correct string usage for JS code
The JavaScript code must be passed as a string, so quotes are needed around 'return document.title'.Step 2: Verify the return keyword usage
To get a value back from JS, 'return' must be used inside the string.Final Answer:
driver.execute_script('return document.title') -> Option AQuick Check:
JS code as string with return keyword [OK]
- Passing JS code without quotes
- Omitting the return keyword when expecting a value
- Using invalid syntax without quotes or return
title = driver.execute_script('return document.title')
print(title)Solution
Step 1: Understand what execute_script returns
When JavaScript code returns a value, execute_script returns it to Python.Step 2: Analyze the JS code 'return document.title'
This returns the page title as a string, so the variable 'title' holds that string.Final Answer:
Prints the current page's title as a string -> Option CQuick Check:
execute_script returns JS value [OK]
- Assuming execute_script returns None always
- Confusing JS code string syntax
- Expecting execute_script to print automatically
driver.execute_script(return document.body.style.backgroundColor = 'red')
Solution
Step 1: Check the argument passed to execute_script
The JavaScript code must be passed as a string, but here it is passed without quotes.Step 2: Confirm method name and JS validity
The method name is correct and changing background color with JS is valid.Final Answer:
Missing quotes around the JavaScript code string -> Option BQuick Check:
JS code must be a quoted string [OK]
- Forgetting quotes around JS code
- Thinking method name is wrong
- Believing JS cannot change styles
Solution
Step 1: Understand scrolling methods in JavaScript
window.scrollBy(x, y)scrolls by x and y pixels relative to current position.Step 2: Identify correct parameters for scrolling down
To scroll down 500 pixels, x=0 and y=500 is correct.Final Answer:
driver.execute_script('window.scrollBy(0, 500)') -> Option DQuick Check:
scrollBy(0, 500) scrolls down 500px [OK]
- Swapping x and y values
- Using scrollTo instead of scrollBy incorrectly
- Passing wrong parameters causing horizontal scroll
Ready to prove it?
Pass the test and this pattern moves unknown
