Scrolling to top of the page in Python using Selenium

后端 未结 6 1424
悲哀的现实
悲哀的现实 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 08:07

    There are 4 ways to scroll up or down

    1)scroll by pixel

    driver.execute_script("window.scrollBy(0,0)","")
    

    2)scroll down until the element is not found

    element=driver.find_element(By.XPATH,"xpath of element")
    driver.execute_script("arguments[0].scrollIntoView();",element)
    

    3)scroll till the end of the page

    driver.execute_script("window.scrollBy(0,document.body.scrollHeight)")
    
    1. using Action Chains

      elementpos=driver.find_element(By.XPATH,"xpath of element")
      actions=ActionChains(driver) actions.move_to_element(elementpos).perform()

提交回复
热议问题