Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to execute JavaScript that returns the page title.
Selenium Java
String title = (String) ((JavascriptExecutor) driver).executeScript([1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the return keyword causes null or undefined results.
Using window.title instead of document.title.
✗ Incorrect
JavaScript execution requires the return keyword to get the value back to Java.
2fill in blank
mediumComplete the code to scroll the page down by 500 pixels using JavaScript.
Selenium Java
((JavascriptExecutor) driver).executeScript([1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping x and y values causes horizontal scroll instead.
Using scrollTo instead of scrollBy changes absolute position.
✗ Incorrect
window.scrollBy(0, 500); scrolls vertically down by 500 pixels.
3fill in blank
hardFix the error in JavaScript execution that tries to click an element but fails.
Selenium Java
((JavascriptExecutor) driver).executeScript([1], element); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names directly inside the script causes errors.
Calling click on arguments object instead of the element.
✗ Incorrect
In JavaScript execution, arguments[0] refers to the passed element.
4fill in blank
hardFill both blanks to return the inner text of an element and trim whitespace.
Selenium Java
String text = (String) ((JavascriptExecutor) driver).executeScript("return arguments[0].[1].[2]();", element);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
textContent may include hidden text.Using
strip() is not a JavaScript string method.✗ Incorrect
innerText gets visible text, and trim() removes extra spaces.
5fill in blank
hardFill all three blanks to create a map of element IDs to their text content using JavaScript execution.
Selenium Java
Map<String, String> texts = (Map<String, String>) ((JavascriptExecutor) driver).executeScript( "let result = {}; for(let el of arguments[0]) { result[el.[1]] = el.[2].[3](); } return result;", elements );
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
textContent instead of innerText includes hidden text.Forgetting to call
trim() leaves extra spaces.✗ Incorrect
Use id for keys, innerText for text, and trim() to clean spaces.