Challenge - 5 Problems
JavaScript Executor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this JavaScript executor call in Selenium?
Consider the following Python Selenium code snippet that uses JavaScript executor to get the page title. What will be printed?
Selenium Python
title = driver.execute_script('return document.title;') print(title)
Attempts:
2 left
💡 Hint
Remember that execute_script can return values from JavaScript to Python.
✗ Incorrect
The JavaScript code 'return document.title;' returns the page title string to Python, so the variable 'title' holds that string and prints it.
❓ assertion
intermediate2:00remaining
Which assertion correctly verifies the page background color using JavaScript executor?
You want to check if the page background color is exactly 'rgb(255, 255, 255)'. Which Python assertion with JavaScript executor is correct?
Selenium Python
color = driver.execute_script('return window.getComputedStyle(document.body).backgroundColor;')Attempts:
2 left
💡 Hint
Use the equality operator '==' for value comparison in assertions.
✗ Incorrect
Option B uses the correct equality operator '==' to compare the string values. Option B uses assignment '=' which is invalid in assert. Option B asserts the opposite. Option B uses 'is' which checks identity, not value equality.
❓ locator
advanced2:00remaining
Which JavaScript executor code correctly scrolls to an element by its ID?
You want to scroll the page to an element with id 'footer' using JavaScript executor in Selenium Python. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Use the correct JavaScript method to scroll an element into view.
✗ Incorrect
Option A correctly uses getElementById and scrollIntoView(). Option A uses scrollTo() which is not a method of elements. Option A uses a non-existent method scrollToView(). Option A uses querySelector with 'footer' which selects elements by tag name, not id.
🔧 Debug
advanced2:00remaining
Why does this JavaScript executor call raise an error?
This Python Selenium code raises an error. Identify the cause.
Selenium Python
driver.execute_script('return document.querySelectorAll(".btn").forEach(el => el.click())')Attempts:
2 left
💡 Hint
Check what the forEach method returns in JavaScript.
✗ Incorrect
The forEach method returns undefined in JavaScript. Returning undefined from execute_script is allowed but if Python expects a value, it may cause issues. The error is likely due to returning undefined. The arrow function syntax is correct, and querySelectorAll exists. Python can execute any JavaScript string.
❓ framework
expert3:00remaining
In Selenium Python, which approach correctly waits for an element to be clickable before clicking it using JavaScript executor?
You want to wait until an element with id 'submit' is clickable, then click it using JavaScript executor. Which code snippet correctly implements this?
Selenium Python
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)
Attempts:
2 left
💡 Hint
Wait for the element to be clickable before clicking it.
✗ Incorrect
Option A waits for the element to be clickable, which means visible and enabled, then clicks it via JavaScript executor. Option A clicks before waiting, which may fail. Option A waits only for presence, not clickability. Option A waits for visibility but not necessarily enabled state.