How to scroll a web page in Selenium pytest on LambdaTest?
Hi Miro!
To test scrolling of a web page in Selenium pytest testing, you can use the following steps -
- Scroll to the bottom of a web page
Below is the line of code to Scroll to the bottom of a web page -
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
- Infinite Scroll
Below is the line of code to scroll a page with infinite loading -
SCROLL_PAUSE_TIME = 0.5
# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
#controls how many times scrolled to bottom
scroll_pass = 0
#change to True for infinite scroll
while scroll_pass < 10:
# Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
scroll_passs+=1
Here is the GitHub repo to help you test scrolling of a web page -