Bird
Raised Fist0
Selenium Javatesting~10 mins

Executing JavaScript in Selenium Java - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to execute JavaScript that returns the page title.

Selenium Java
JavascriptExecutor js = (JavascriptExecutor) driver;
String title = (String) js.executeScript([1]);
Drag options to blanks, or click blank then click option'
A"return document.title;"
B"document.title;"
C"return window.title;"
D"window.document.title"
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting 'return' causes the JavaScript to not return the title.
Using 'window.title' instead of 'document.title' is incorrect.
2fill in blank
medium

Complete the code to scroll the page down by 500 pixels using JavaScript.

Selenium Java
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript([1]);
Drag options to blanks, or click blank then click option'
A"window.scrollBy(0, 500);"
B"scrollTo(0, 500);"
C"window.scrollTo(500, 0);"
D"scrollBy(500, 0);"
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping x and y values causes horizontal instead of vertical scroll.
Using 'scrollTo' without 'window.' may cause errors.
3fill in blank
hard

Fix the error in the code to click an element using JavaScript.

Selenium Java
WebElement element = driver.findElement(By.id("submitBtn"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript([1], element);
Drag options to blanks, or click blank then click option'
A"click(arguments[0]);"
B"element.click();"
C"arguments[0].click();"
D"arguments.click();"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'element.click()' inside the script string causes JavaScript errors.
Incorrect use of 'arguments' object leads to runtime errors.
4fill in blank
hard

Fill both blanks to return the inner text of an element found by CSS selector.

Selenium Java
JavascriptExecutor js = (JavascriptExecutor) driver;
String text = (String) js.executeScript("return document.querySelector([1]).[2];");
Drag options to blanks, or click blank then click option'
A"#main-content"
B"innerText"
C"textContent"
D".content"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class selector without quotes causes syntax errors.
Using 'textContent' instead of 'innerText' changes the returned text.
5fill in blank
hard

Fill all three blanks to create a map of element IDs to their text content for elements with class 'item'.

Selenium Java
JavascriptExecutor js = (JavascriptExecutor) driver;
Map<String, String> texts = (Map<String, String>) js.executeScript(
  "const elements = document.querySelectorAll([1]);"
  + "const result = {};"
  + "elements.forEach(el => { result[el.[2]] = el.[3]; });"
  + "return result;"
);
Drag options to blanks, or click blank then click option'
A".item"
B"id"
C"textContent"
D"className"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'className' instead of 'id' as key causes incorrect mapping.
Selecting elements without quotes causes JavaScript errors.

Practice

(1/5)
1. What is the main purpose of using JavascriptExecutor in Selenium Java?
easy
A. To locate elements on the page using JavaScript selectors
B. To execute JavaScript code directly in the browser during a test
C. To start the Selenium WebDriver session
D. To generate test reports automatically

Solution

  1. Step 1: Understand the role of JavascriptExecutor

    JavascriptExecutor allows running JavaScript code inside the browser from Selenium tests.
  2. Step 2: Identify the main use case

    It is mainly used to perform actions or retrieve information that Selenium WebDriver cannot do directly.
  3. Final Answer:

    To execute JavaScript code directly in the browser during a test -> Option B
  4. Quick Check:

    JavascriptExecutor runs JS code in browser [OK]
Hint: Remember: JavascriptExecutor runs JS code inside browser [OK]
Common Mistakes:
  • Confusing JavascriptExecutor with element locators
  • Thinking it starts WebDriver sessions
  • Assuming it generates reports
2. Which of the following is the correct way to cast a WebDriver instance to JavascriptExecutor in Selenium Java?
easy
A. JavascriptExecutor js = driver.executeScript();
B. JavascriptExecutor js = driver.getJavascriptExecutor();
C. JavascriptExecutor js = new JavascriptExecutor(driver);
D. JavascriptExecutor js = (JavascriptExecutor) driver;

Solution

  1. Step 1: Recall the casting syntax in Java

    To use JavascriptExecutor, you cast the WebDriver instance like this: (JavascriptExecutor) driver.
  2. Step 2: Check each option

    JavascriptExecutor js = (JavascriptExecutor) driver; uses correct casting syntax. The other options are invalid method calls or constructors.
  3. Final Answer:

    JavascriptExecutor js = (JavascriptExecutor) driver; -> Option D
  4. Quick Check:

    Cast driver to JavascriptExecutor using (JavascriptExecutor) [OK]
Hint: Casting uses (JavascriptExecutor) before driver [OK]
Common Mistakes:
  • Using non-existent methods like getJavascriptExecutor()
  • Trying to instantiate JavascriptExecutor with new keyword
  • Calling executeScript() without casting
3. What will be the output of the following code snippet?
JavascriptExecutor js = (JavascriptExecutor) driver;
String title = (String) js.executeScript("return document.title;");
System.out.println(title);
medium
A. Prints the current page's title
B. Throws a ClassCastException
C. Prints 'undefined'
D. Prints the URL of the page

Solution

  1. Step 1: Understand the JavaScript executed

    The script returns document.title, which is the page's title as a string.
  2. Step 2: Check the casting and output

    The returned object is cast to String and printed, so it outputs the page title.
  3. Final Answer:

    Prints the current page's title -> Option A
  4. Quick Check:

    executeScript returns document.title string [OK]
Hint: executeScript returns JS value; cast to String for title [OK]
Common Mistakes:
  • Expecting URL instead of title
  • Not casting return value to String
  • Confusing output with undefined or errors
4. Identify the error in the following code snippet:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();");
medium
A. Incorrect casting of driver to JavascriptExecutor
B. executeScript method does not exist
C. Missing WebElement argument to click()
D. JavaScript syntax error in the string

Solution

  1. Step 1: Analyze the executeScript call

    The script uses arguments[0].click(); which requires a WebElement passed as an argument.
  2. Step 2: Check the method call parameters

    No WebElement is passed to executeScript, so it will fail at runtime.
  3. Final Answer:

    Missing WebElement argument to click() -> Option C
  4. Quick Check:

    arguments[0] needs a WebElement passed [OK]
Hint: Pass WebElement as argument when using arguments[0] [OK]
Common Mistakes:
  • Forgetting to pass WebElement argument
  • Assuming casting is wrong
  • Thinking executeScript method is missing
5. You want to scroll a page to a specific WebElement using JavaScript in Selenium Java. Which code snippet correctly achieves this?
hard
A. js.executeScript("arguments[0].scrollIntoView(true);", element);
B. js.executeScript("window.scrollTo(element);");
C. js.executeScript("scrollToElement(arguments[0]);", element);
D. js.executeScript("document.scrollTo(arguments[0]);");

Solution

  1. Step 1: Recall JavaScript method to scroll to element

    The correct JS method is scrollIntoView(true) called on the element.
  2. Step 2: Check the Selenium executeScript usage

    Pass the WebElement as an argument and use arguments[0].scrollIntoView(true); to scroll.
  3. Final Answer:

    js.executeScript("arguments[0].scrollIntoView(true);", element); -> Option A
  4. Quick Check:

    Use scrollIntoView with element argument [OK]
Hint: Use arguments[0].scrollIntoView(true) with element [OK]
Common Mistakes:
  • Using window.scrollTo with element (invalid)
  • Calling non-existent scrollToElement function
  • Not passing element as argument
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard