0
0
Selenium Javatesting~10 mins

Why JavaScript execution handles edge cases in Selenium Java - Test Your Understanding

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
String title = (String) ((JavascriptExecutor) driver).executeScript([1]);
Drag options to blanks, or click blank then click option'
A"return window.title;"
B"document.title;"
C"return document.title;"
D"window.document.title"
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the return keyword causes null or undefined results.
Using window.title instead of document.title.
2fill in blank
medium

Complete 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'
A"scrollTo(0, 500);"
B"window.scrollBy(0, 500);"
C"window.scroll(500, 0);"
D"scrollBy(500, 0);"
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping x and y values causes horizontal scroll instead.
Using scrollTo instead of scrollBy changes absolute position.
3fill in blank
hard

Fix 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'
A"arguments[0].click();"
B"click(arguments[0]);"
C"element.click();"
D"arguments.click();"
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names directly inside the script causes errors.
Calling click on arguments object instead of the element.
4fill in blank
hard

Fill 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'
AinnerText
Btrim
CtextContent
Dstrip
Attempts:
3 left
💡 Hint
Common Mistakes
Using textContent may include hidden text.
Using strip() is not a JavaScript string method.
5fill in blank
hard

Fill 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'
Aid
BinnerText
Ctrim
DtextContent
Attempts:
3 left
💡 Hint
Common Mistakes
Using textContent instead of innerText includes hidden text.
Forgetting to call trim() leaves extra spaces.