Skip to content
Advertisement

How to know scroll bar is at end in selenium python

I am implementing a while loop in selenium, and want to condition my while loop, so when the scroll bar is at its end of its scroll the while loop should stop. How can i code this type of condition in while loop? Iam using Keys.DOWN and my while loop is right now set to True

My code of while loop:

        while True:
            self.driver.find_element_by_id('pane-side').send_keys(Keys.DOWN * 5)
            self.driver.find_elements_by_xpath("//div[@class='_2wP_Y']")

Advertisement

Answer

Here is the pseudo code that should do the trick.

Check if the current (scrolled position+ window height) is greater than page height(-1)

pageHeight = driver.execute_script("return document.body.scrollHeight")
totalScrolledHeight = driver.execute_script("return window.pageYOffset + window.innerHeight")

# -1 is to make sure the rounding issues
if((pageHeight-1)<=totalScrolledHeight):
   print("pass")
else:
  print("Failed")

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement