I am trying to scroll to the end of a page so that I can make all the data visible and extract it. I tried to find a command for it but it's available in java (driver.executeScript) but couldn't find for python. Right now I am making the computer press the end key thousand times:
while i<1000: scroll = driver.find_element_by_tag_name('body').send_keys(Keys.END) i+=1
And I also tried driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
but it scrolls to the end of the loaded page and the same thing END key does. Once at the bottom of the page, next content loads. But now it doesn't scroll again.
I know there will be a very nice alternative for this.
How do I scroll to the end of the page using selenium in Python?
Well I finally figured out a solution:
lenOfPage = driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;") match=False while(match==False): lastCount = lenOfPage time.sleep(3) lenOfPage = driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;") if lastCount==lenOfPage: match=True
This can be done in one line by scrolling to document.body.scrollHeight
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With