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:
JavaScript
x
4
1
while True:
2
self.driver.find_element_by_id('pane-side').send_keys(Keys.DOWN * 5)
3
self.driver.find_elements_by_xpath("//div[@class='_2wP_Y']")
4
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)
JavaScript
1
10
10
1
pageHeight = driver.execute_script("return document.body.scrollHeight")
2
totalScrolledHeight = driver.execute_script("return window.pageYOffset + window.innerHeight")
3
4
# -1 is to make sure the rounding issues
5
if((pageHeight-1)<=totalScrolledHeight):
6
print("pass")
7
else:
8
print("Failed")
9
10