0
0
Selenium Pythontesting~20 mins

Scrolling with JavaScript in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JavaScript Scrolling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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?
Selenium Python
driver.execute_script("window.scrollTo(0, 500)")
scroll_position = driver.execute_script("return window.pageYOffset")
print(scroll_position)
A0
B500
C1000
DNone (raises an error)
Attempts:
2 left
💡 Hint
window.scrollTo(x, y) scrolls the page to the coordinates (x, y).
assertion
intermediate
1: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?
Selenium Python
scroll_pos = driver.execute_script("return window.pageYOffset")
Aassert scroll_pos == 300
Bassert scroll_pos > 300
Cassert scroll_pos >= 300
Dassert scroll_pos < 300
Attempts:
2 left
💡 Hint
Use equality to check exact scroll position.
locator
advanced
2: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?
Adriver.find_element(By.ID, 'submit')
Bdriver.find_element(By.CLASS_NAME, 'btn')
Cdriver.find_element(By.XPATH, "//button[text()='Submit']")
Ddriver.find_element(By.TAG_NAME, 'button')
Attempts:
2 left
💡 Hint
Use a locator that matches the exact button text.
🔧 Debug
advanced
2: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?
AThe page is already at the bottom and cannot scroll further
BThe scrollBy method only works with negative values
CThe driver.execute_script method cannot run scrollBy
DThe scrollBy method requires numeric arguments, not strings
Attempts:
2 left
💡 Hint
Check the argument types passed to scrollBy.
framework
expert
3: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?
Selenium Python
def scroll_to_element(driver, element):
    # Fill in the correct JavaScript command
Adriver.execute_script("arguments[0].scrollIntoView();", element)
Bdriver.execute_script("window.scrollBy(0, element.offsetTop);")
Cdriver.execute_script("element.scrollIntoView();")
Ddriver.execute_script("window.scrollTo(arguments[0].offsetTop, 0);", element)
Attempts:
2 left
💡 Hint
Use JavaScript's scrollIntoView with the element argument.