程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python selenium combines with java script mouse and keyboard to control scroll bars

編輯:Python

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)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved