from selenium import webdriver

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.common.action_chains import ActionChains

import time

driver = webdriver.Firefox()

driver.get('https://www.baidu.com')

driver.maximize_window()

driver.find_element_by_id('kw').send_keys('python')

driver.find_element_by_id('su').click()

time.sleep(5)

# first method

# js = "window.scrollTo(0,document.body.scrollHeight)"

# driver.execute_script(js)

# time.sleep(3)

# js = "window.scrollTo(0,0)"

# driver.execute_script(js)

# second

# js = "var q=document.documentElement.scrollTop=10000"

# driver.execute_script(js)

# time.sleep(3)

# js = "var q=document.documentElement.scrollTop=0"

# driver.execute_script(js)

# 震动条拉到指定场所(简直元素)

target = driver.find_element_by_id("rs")

driver.execute_script("arguments[0].scrollIntoView();", target)

#将网页震动到网页底部

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

# 向下震动200个像素,鼠标场所也随着变了

driver.execute_script("window.scrollBy(0,200)", "")

# 一致于按键盘上的PgDn按键,不妨换成Keys.DOWN(下箭镞,然而这个下拉进度比拟慢)

for i in range(6):

ActionChains(driver).send_keys(Keys.PAGE_DOWN).perform()

time.sleep(1)

time.sleep(3)

driver.close()