When browsing the web , Scroll the vertical scroll bar with the mouse , It is commonly used. . stay Selenium Automated testing framework , You can go through and JS ( Java Script ), ActionChains In combination with implementation . Here are some examples 4 The method of Planting , Take Sina for example .
The following related methods are used to test the video Please have a look at https://weibo.com/2203755810
1. JS Of scrollTo() and scrollBy() Method
#1
Move directly to the absolute position of the specified coordinates .
#2
Move directly to the relative specified coordinate position , This coordinate is relative to (0,1000) This absolute position moves down again 300 Pixels .
#3
Move down again 300 Pixels , That is, relative to (0,1000) This absolute coordinate position , Move 600 Pixels .
Also through while perhaps for The number of cycles to scroll down , You can reduce the amount of duplicate code .
notes : In video to move 5 Time, for example .
#4
Scroll bar to the bottom
#5
Drag the scroll bar to the top of the browser
from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.get(‘http://www.sina.com.cn’)
browser.maximize_window()
time.sleep(3)
#1
js = ‘window.scrollTo(0,1000)’
browser.execute_script(js)
time.sleep(5)
#2
js = ‘window.scrollBy(0,300)’
browser.execute_script(js)
time.sleep(5)
#3
js = ‘window.scrollBy(0,300)’
browser.execute_script(js)
time.sleep(5)
#4
js = ‘window.scrollTo(0, document.body.scrollHeight)’
browser.execute_script(js)
time.sleep(5)
#5
js = ‘window.scrollTo(0,0)’
browser.execute_script(js)
2. scrollIntoView() and scrollIntoView(false) Method
#1
location “ game ”, Here's the picture 1
#2
use scrollIntoView() Method , The mouse scrolls to " game " Apex Align with the top of the current window , Here's the picture 2
#3
use scrollIntoView(false) Method scroll the mouse to " game " Bottom Align with the bottom of the current window Here's the picture 3
import time
from selenium import webdriver
browser = webdriver.Chrome()
browser.maximize_window()
browser.get(‘https://www.sina.com.cn’)
time.sleep(3)
#1
element = browser.find_element_by_xpath(’//*[@id=“SI_Order_L”]/div/div[1]/div/span[1]/a’)
#2
browser.execute_script(‘arguments[0].scrollIntoView()’, element)
time.sleep(3)
#3
browser.execute_script(‘arguments[0].scrollIntoView(false)’, element)
chart 1
chart 2
chart 3
3. key_down() and key_up() Method Combined with the circular method
#1
Press 5 Time Page_Down key
#2
Press 5 Time Page_Up key
#3
Press End Key to return to the bottom
#4
Press Home Key to return to the top
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Chrome()
browser.maximize_window()
browser.get(‘http://www.sina.com.cn’)
time.sleep(2)
#1
for i in range(5):
ActionChains(browser).key_down(Keys.PAGE_DOWN).key_up(Keys.PAGE_DOWN).perform()
time.sleep(2)
#2
for i in range(5):
ActionChains(browser).key_down(Keys.PAGE_UP).key_up(Keys.PAGE_UP).perform()
time.sleep(1)
#3
ActionChains(browser).key_down(Keys.END).key_up(Keys.END).perform()
time.sleep(2)
#4
ActionChains(browser).key_down(Keys.HOME).key_up(Keys.HOME).perform()
4. JS Of scrollTop() Method
#1
Variable value is 1000, Indicates scrolling to the bottom .
#2
Variable value is 0, Means to scroll to the top
from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.maximize_window()
browser.get(‘http://www.sina.com.cn’)
time.sleep(2)
#1
js=“var q=document.documentElement.scrollTop=10000”
browser.execute_script(js)
time.sleep(2)
#2
js=“var q=document.documentElement.scrollTop=0”
browser.execute_script(js)