from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
#driver.maximize_window()
sleep(3)
#移动到绝对位置
js = "window.scrollTo(0,1000);"
driver.execute_script(js)
sleep(5)
#移动到相对位置,移动300,再移动300
js = "window.scrollBy(0,300);"
driver.execute_script(js)
sleep(5)
js = "window.scrollBy(0,300);"
driver.execute_script(js)
sleep(5)
#滚动条拖动到浏览器的最下方
js = "window.scrollTo(0, document.body.scrollHeight)"
driver.execute_script(js)
sleep(5)
#滚动条拖动到浏览器的最上方
js = "window.scrollTo(0,0);"
driver.execute_script(js)
sleep(5)
element = driver.find_element_by_xpath("//*[@id='index_hot_focus']/div[1]/div/h2")
#移动到元素element对象的“顶端”与当前窗口的“顶部”对齐
driver.execute_script("arguments[0].scrollIntoView();", element)
sleep(5)
#移动到元素element对象的“底端”与当前窗口的“底部”对齐
driver.execute_script("arguments[0].scrollIntoView(false);", element)
driver.get_screenshot_as_png()
sleep(5)
driver.quit()
selenium操作滚动条
最新推荐文章于 2024-07-02 22:35:34 发布