Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The JavaScript code must return the document title explicitly using 'return document.title;'.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping x and y values causes horizontal instead of vertical scroll.
Using 'scrollTo' without 'window.' may cause errors.
✗ Incorrect
The correct JavaScript to scroll down vertically by 500 pixels is 'window.scrollBy(0, 500);'.
3fill in blank
hardFix 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'
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.
✗ Incorrect
To click an element via JavaScript, use 'arguments[0].click();' passing the element as argument.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class selector without quotes causes syntax errors.
Using 'textContent' instead of 'innerText' changes the returned text.
✗ Incorrect
Use a CSS selector string like '#main-content' and property 'innerText' to get visible text.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'className' instead of 'id' as key causes incorrect mapping.
Selecting elements without quotes causes JavaScript errors.
✗ Incorrect
Select elements by '.item', use 'id' as key and 'textContent' as value in the map.