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

后端 未结 6 1622
梦毁少年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:37

    You can try below code to scroll page up:

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

    Another way (preferred) you can scroll up to required element:

    element = self.web_driver.find_element_by_xpath('SOME_XPATH') # you can use ANY way to locate element
    coordinates = element.location_once_scrolled_into_view # returns dict of X, Y coordinates
    self.web_driver.execute_script('window.scrollTo({}, {});'.format(coordinates['x'], coordinates['y']))
    

提交回复
热议问题