Challenge - 5 Problems
JavaScript Scrolling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the effect of this JavaScript scroll command in Selenium?
Consider the following Selenium Python code snippet that uses JavaScript to scroll a webpage.
What will be the vertical scroll position of the page after executing this code?
What will be the vertical scroll position of the page after executing this code?
Selenium Python
driver.execute_script("window.scrollTo(0, 500)") scroll_position = driver.execute_script("return window.pageYOffset") print(scroll_position)
Attempts:
2 left
💡 Hint
window.scrollTo(x, y) scrolls the page to the coordinates (x, y).
✗ Incorrect
The JavaScript command window.scrollTo(0, 500) scrolls the page vertically to 500 pixels from the top. The pageYOffset property returns the number of pixels the document is currently scrolled vertically. So, the output will be 500.
❓ assertion
intermediate1:30remaining
Which assertion correctly verifies the page scrolled down by 300 pixels?
You want to check if the page has scrolled down by exactly 300 pixels using Selenium and JavaScript.
Which assertion statement correctly verifies this?
Which assertion statement correctly verifies this?
Selenium Python
scroll_pos = driver.execute_script("return window.pageYOffset")Attempts:
2 left
💡 Hint
Use equality to check exact scroll position.
✗ Incorrect
To verify the page scrolled exactly 300 pixels, the assertion must check equality (== 300). Using > or >= would allow values greater than 300, which is not exact.
❓ locator
advanced2:00remaining
Which locator is best to scroll to a specific element before interacting?
You want to scroll to a button with text 'Submit' before clicking it using Selenium Python.
Which locator strategy is best to find this button for scrolling?
Which locator strategy is best to find this button for scrolling?
Attempts:
2 left
💡 Hint
Use a locator that matches the exact button text.
✗ Incorrect
Using XPath with the exact button text ensures you find the correct button to scroll to. ID or class may not be unique or present. Tag name alone is too broad.
🔧 Debug
advanced2:30remaining
Why does this scroll command fail to scroll the page?
You run this Selenium Python code but the page does not scroll:
driver.execute_script("window.scrollBy(0, '300')")
What is the reason?
driver.execute_script("window.scrollBy(0, '300')")
What is the reason?
Attempts:
2 left
💡 Hint
Check the argument types passed to scrollBy.
✗ Incorrect
The scrollBy method expects numbers for x and y offsets. Passing '300' as a string causes the scroll to fail silently.
❓ framework
expert3:00remaining
How to implement a reusable scroll helper in Selenium Python?
You want to create a helper function in Selenium Python that scrolls to any element passed to it using JavaScript.
Which function implementation correctly scrolls to the element?
Which function implementation correctly scrolls to the element?
Selenium Python
def scroll_to_element(driver, element): # Fill in the correct JavaScript command
Attempts:
2 left
💡 Hint
Use JavaScript's scrollIntoView with the element argument.
✗ Incorrect
The correct way is to pass the element as an argument and call scrollIntoView on it. Option A does this correctly. Options A and D misuse arguments or syntax. Option A misses passing the element.