Scroll up and down to get Element into View with Selenium Python

后端 未结 6 1618
梦毁少年i
梦毁少年i 2021-01-05 19:10

I need to be able to scroll up and after that down to find some element with Selenium.
I already saw many questions and answers, the main idea I found is self.web_

6条回答
  •  长发绾君心
    2021-01-05 19:48

    If you defined driver as webdriver.Chrome() or webdriver.Firefox(),you can use this code for scrolling up:

    from selenium.webdriver.common.keys import Keys
    driver.find_element_by_tag_name('body').send_keys(Keys.HOME)
    

    Also, for scrolling down, you can try this code:

    driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", scr1)
    

    scr1 is your box xpath which you want to scroll it. You can scroll to middle of the page by this code, too:

    driver.execute_script("arguments[0].scrollTop = arguments[0].scrollWidth", scr1)
    

提交回复
热议问题