Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.7k points)

I am currently using selenium WebDriver to parse through Facebook user friends page and extract all ids from the AJAX script. But I need to scroll down to get all the friends. How can I scroll down in Selenium. I am using python.

1 Answer

0 votes
by (62.9k points)

You can use:

driver.execute_script("window.scrollTo(0, Y)")

where Y is the height (on a full HD monitor it's 1080). 

You can also use

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

to scroll to the bottom of the page.

If you want to scroll to a page with infinite loading, maybe in social networking sites like facebook, etc.


# Get scroll height last_height = driver.execute_script("return document.body.scrollHeight")

while True: 

# Scroll down to bottom

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

# Wait to load page 


# Calculate new scroll height and compare with last scroll height

new_height = driver.execute_script("return document.body.scrollHeight")

if new_height == last_height:


last_height = new_height


Hope this helps!

If you wish to Learn Selenium web driver visit, this Selenium Webdriver Tutorial and Selenium Web Driver Interview Questions.

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s automation testing course!

Browse Categories