Home > Mobile >  python scroll a specific scrollbar vertically
python scroll a specific scrollbar vertically

Time:02-02

I am using python selenium to scroll a webpage, i am trying to get to the bottom of the page by clicking on the scroll element but it's returning this error: MoveTargetOutOfBoundsException: move target out of bounds

My code so far:

from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
scrollbar_needed = driver.find_element_by_xpath("//div[@class='antiscroll-scrollbar antiscroll-scrollbar-vertical antiscroll-scrollbar-shown']")
actions.click_and_hold(scrollbar_needed).move_by_offset(0,5000).release().perform()

Is there another way to scroll using the path of "scrollbar_needed"?

CodePudding user response:

There are couple of ways of scrolling through a dynamic webpage.

option 1: If you want to scroll a specific limit you can use this

driver.execute_script("window.scrollTo(0, 1000);")
      

option 2: If you want to scroll and reach to end of the page you can use this

driver.find_element(By.TAG_NAME,'body').send_keys(Keys.END)
      

option 3: If it is lazy loading page you can use that option as well.

     # Get scroll height
      last_height = driver.execute_script("return document.body.scrollHeight")
      limit=500
      while True:
         # Scroll down to to a limit
         driver.execute_script("window.scrollTo(0,"   limit   ");")

         # Wait to load page
         time.sleep(1)
         # increase the scroll height
         limit=limit 500
         # Calculate new scroll height and compare with last scroll height
         new_height = driver.execute_script("return document.body.scrollHeight")
         if new_height == last_height:
             break
         last_height = new_height  

CodePudding user response:

Here it is:

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