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

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

    You can do it with also getBoundingClientRect() and scrollTo()

    self.web_driver.execute_script("coordinates = arguments[0].getBoundingClientRect();scrollTo(coordinates.x,coordinates.y);", element)
    

    I tested and it goes up and down.

    0 讨论(0)
  • 2021-01-05 19:36

    This was very useful for me:

    WebElement scrolling = new WebDriverWait(driver, 10)
                .until(ExpectedConditions.visibilityOfElementLocated(By.className("classSomething")));
    
    //SCroll Down
    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollTo(0, arguments[0].scrollHeight)", scrolling);
    
    //Scroll Up
    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollTo(0, arguments[0].scrollUp)", scrolling);
    
    0 讨论(0)
  • 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']))
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2021-01-05 19:53

    Try this, it works good:

    view_port_height = "var viewPortHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);"
    element_top = "var elementTop = arguments[0].getBoundingClientRect().top;"
    js_function = "window.scrollBy(0, elementTop-(viewPortHeight/2));"
    
    scroll_into_middle = view_port_height + element_top + js_function
    
    driver.execute_script(scroll_into_middle, element)
    
    0 讨论(0)
  • 2021-01-05 19:54

    I have discovered a handy tricky hacky stuff over the years (I've been using Selenium for 150 years).

    Whenever you're unable to scroll up a form, just send keys into an input on the top of the page. The driver will find it and will automagically scroll up the page to perform the action.

    Whenever I think about this trick, I realize being an old man isn't that bad.

    Good luck you fresh sailor, see you on the shores of Selenia.

    0 讨论(0)
提交回复
热议问题