0
0
Selenium Javatesting~20 mins

Scrolling into view in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Scrolling Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Selenium Java code snippet?
Consider the following Selenium Java code that scrolls an element into view and then clicks it. What will be the output if the element is not present on the page?
Selenium Java
WebElement element = driver.findElement(By.id("nonexistent"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
element.click();
ANoSuchElementException is thrown at findElement line
BElementNotInteractableException is thrown at click line
CJavascriptException is thrown at executeScript line
DNo exception, element is clicked successfully
Attempts:
2 left
💡 Hint
Think about what happens when findElement cannot find the element.
assertion
intermediate
2:00remaining
Which assertion correctly verifies that an element is scrolled into view?
You want to assert that a WebElement is visible in the viewport after scrolling it into view using JavaScript. Which assertion is correct?
Selenium Java
WebElement element = driver.findElement(By.id("target"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
AassertTrue((Boolean)((JavascriptExecutor) driver).executeScript("return (arguments[0].getBoundingClientRect().top >= 0) && (arguments[0].getBoundingClientRect().bottom <= window.innerHeight);", element));
BassertEquals(element.getLocation().getY(), 0);
CassertTrue(element.isDisplayed());
DassertFalse(element.isEnabled());
Attempts:
2 left
💡 Hint
Visibility alone does not guarantee element is in viewport. Use JavaScript to check bounding rectangle.
🔧 Debug
advanced
2:00remaining
Why does this scrolling code fail to bring the element into view?
This Selenium Java code tries to scroll an element into view but the element remains hidden. What is the likely cause?
Selenium Java
WebElement element = driver.findElement(By.cssSelector(".hidden-element"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(false);", element);
AJavascriptExecutor cannot scroll elements with class selectors
BscrollIntoView(false) scrolls the element to the top of the viewport, hiding it
CThe selector is invalid, so element is null
DThe element has CSS property display:none, so it cannot be scrolled into view
Attempts:
2 left
💡 Hint
Check the CSS properties of the element that affect visibility.
framework
advanced
2:00remaining
Which Selenium Java framework method best ensures an element is scrolled into view before clicking?
You want to create a reusable method in your Selenium Java framework that scrolls an element into view and clicks it safely. Which method implementation is best?
A
public void scrollAndClick(WebElement element) {
  new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(element));
  element.click();
}
B
public void scrollAndClick(WebElement element) {
  ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
  new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(element));
  element.click();
}
C
public void scrollAndClick(WebElement element) {
  ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
  element.click();
}
D
public void scrollAndClick(WebElement element) {
  element.click();
  ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
}
Attempts:
2 left
💡 Hint
Think about waiting for clickability after scrolling.
🧠 Conceptual
expert
2:00remaining
What is the main reason to use JavaScript scrolling instead of Selenium's Actions class for scrolling into view?
In Selenium Java, why might you prefer using JavaScriptExecutor's scrollIntoView method over Actions class methods to scroll an element into view?
AActions class scrolling can scroll elements that are hidden with display:none
BJavaScript scrolling does not require the element to be present in the DOM
CJavaScript scrollIntoView works reliably even when the element is outside the visible viewport or inside scrollable containers
DActions class scrolling is faster and more reliable than JavaScript scrolling
Attempts:
2 left
💡 Hint
Consider how scrolling behaves inside nested scrollable areas.