selenium-->js滚动操作

import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver =webdriver.Chrome()
driver.maximize_window()
driver.get("http://www.baidu.com")

driver.find_element(By.XPATH,'//input[@id="kw"]').send_keys("python")
driver.find_element(By.XPATH,'//input[@id="su"]').click()

loc =(By.XPATH,"""//span[@class="c-title-text" ]//em[text()="Python"]""")
WebDriverWait(driver,10).until(EC.presence_of_element_located(loc))
ele =driver.find_element(*loc)
#js函数
#滚动条操作,driver.execute_script()
#arguments[0].scrollIntoView(); 将元素移动到可视区域,固定用法
#arguments,接收外部参数,类型为列表
# scrollIntoView(false),false:表示元素与页面可视区域的底部对齐,默认为ture,与页面可视区域的顶部对齐
driver.execute_script("arguments[0].scrollIntoView(false);",ele)
# ele.click()
time.sleep(2)


driver.execute_script("window.scrollTo(0,document.body.scrollHeight)") #滚动到页面底部
time.sleep(2)
driver.execute_script("window.scrollTo(document.body.scrollHeight,0)") #滚动到页面顶部
time.sleep(2)

driver.quit()





上一篇:Windows下vscode编写python,FileNotFoundError: No such file or directory.


下一篇:python系统首页句柄方法