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

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

提交回复
热议问题