0
0
Selenium Javatesting~10 mins

Executing JavaScript in Selenium Java - Interactive Code Practice

Choose your learning style9 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.