1. 程式人生 > >【2018.05.11】python3.6+selenium JS處理滾動條,聚焦元素

【2018.05.11】python3.6+selenium JS處理滾動條,聚焦元素


execute_script(),可以直接執行js的指令碼

--scrollHeight 獲取物件的滾動高度。 
--scrollLeft 設定或獲取位於物件左邊界和視窗中目前可見內容的最左端之間的距離。 
--scrollTop 設定或獲取位於物件最頂端和視窗中可見內容的最頂端之間的距離。 

--scrollWidth 獲取物件的滾動寬度。

#滾動到底部
js = "window.scrollTo(0,document.body.scrollHeight)" 
driver.execute_script(js)

#滾動到頂部


js = "window.scrollTo(0,0)" 
driver.execute_script(js)


#coding = utf-8
'''
滾動條的處理

'''
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
driver.find_element_by_name('tj_trnews'
).click() print(driver.name) #回到頂部 def scroll_top(): js = "window.scrollTo(0,0)" driver.execute_script(js) time.sleep(3) #回到底部 def scroll_foot(): js = "window.scrollTo(0,document.body.scrollHeight)" driver.execute_script(js) time.sleep(3) #聚焦元素 def scroll_jujiao(): target =
driver.find_element_by_xpath('//*[@id="app_tooltip"]')#要聚焦的目標元素 driver.execute_script("arguments[0].scrollIntoView();",target) scroll_foot() scroll_top() scroll_foot() scroll_jujiao()


以上...歡迎交流,一起學習,麻花藤測試開發群728109091