Foreword
When browsing the web, often you need to slide page operation. selenium automate the process, can be achieved by sliding the page execute_script execute js statement. Common sliding into four scenarios: slide to the bottom of the page, to the top of the slide, slide to the specific position, slide to the target element is visible.
Slide to the top
1 js="window.scrollTo(0,-document.body.scrollHeight)" 2 driver.execute_script(js)
Sliding to the bottom
1 js="window.scrollTo(0,document.body.scrollHeight)" 2 driver.execute_script(js)
DETAILED slid into position
. 1 window.scrollBy (0,500) # slide 500 pixels down 2 . 3 window.scrollBy (0, -500) # Scroll up 500 pixels . 4 . 5 window.scrollBy (500,0) # slides to the right 500 pixels . 6 . 7 window.scrollBy (-500,0) # leftward scroll 500 pixels . 8 . 9 driver.execute_script ( " the window.scrollTo (X, Y) " ) # slid into the specific location
Slide to the target element is visible
. 1 driver.execute_script ( " arguments [0] .scrollIntoView (); " , element) # sliding element to the visible element