Scrolling to top of the page in Python using Selenium

后端 未结 6 1412
悲哀的现实
悲哀的现实 2020-11-27 07:30

I\'m having issues with scrolling to the top of the web page when using Python and Selenium.

When the page loads for some reason you are taken to the bottom of the

6条回答
  •  孤城傲影
    2020-11-27 07:43

    from selenium import webdriver

    t=10
    while t:
    
    #if you want to scroll to the end of the page,use this
    
    driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
        sleep(3)
    
    
    #if you want to scroll down upto some level use this, here i used "1000" you may vary 
    #it according to your use
    
    driver.execute_script("scrollBy(0,+1000);")
        sleep(3)
    
    
    #if you want to scroll some level up, use this,again i used here "-500" you may vary 
    #according to your use 
    
    driver.execute_script("scrollBy(0,-500);")
    sleep(3)
    t=t-1       # it`s a part of the loop
    

    This will surely help you :)

提交回复
热议问题