from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
driver=webdriver.Firefox()
driver.get("http://www.baidu.com")
driver.maximize_window()
driver.find_element_by_css_selector("#kw").send_keys("Python")
# 获取搜索框元素对象
element=driver.find_element_by_css_selector("#kw")
sleep(3)
#双击操作
ActionChains(driver).double_click(element).perform()
sleep(2)
#右击操作
ActionChains(driver).context_click(element).perform()
sleep(3)
#鼠标悬停
above=driver.find_element_by_css_selector(".pf")
ActionChains(driver).move_to_element(above).perform()
sleep(4)
driver.quit()
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
driver=webdriver.Firefox()
driver.get("http://www.baidu.com")
driver.find_element_by_css_selector("#kw").send_keys("Python")
sleep(2)
#键盘全选操作 Ctrl+A
driver.find_element_by_css_selector("#kw").send_keys(Keys.CONTROL,'a')
#键盘选择复制或剪切操作 Ctrl+C
driver.find_element_by_css_selector("#kw").send_keys(Keys.CONTROL,'c')
driver.find_element_by_css_selector("#kw").send_keys(Keys.CONTROL,'x')
#打开搜狗页面
driver.get("http://www.sogou.com/")
sleep(2)
#粘贴复制内容
driver.find_element_by_css_selector(".sec-input").send_keys(Keys.CONTROL,'v')
sleep(2)
#点击搜索按钮
# driver.find_element_by_xpath("//input[@id='stb']").click()
driver.find_element_by_css_selector("#stb").click()
sleep(3)
driver.quit()
selenium可以模拟人工上下滑动进度条的操作。
打开我要自学网页面,然后将滚动条拖到最底部,然后再拖到顶部
from selenium import webdriver
from time import sleep
driver=webdriver.Firefox()
driver.get("http://www.51zxw.net/")
sleep(2)
#将滚动调拖到最底部
js="var action=document.documentElement.scrollTop=10000"
driver.execute_script(js)
sleep(2)
#将滚动条拖到最顶部
js="var action=document.documentElement.scrollTop=0"
driver.execute_script(js)
sleep(3)
driver.quit()
分别打开我要自学网页面和百度页面,然后进行截图
from selenium import webdriver
from time import sleep
#加载浏览器驱动
driver=webdriver.Firefox()
#打开自学网页面并截图
driver.get("http://www.51zxw.net")
driver.get_screenshot_as_file(r"E:\Python_script\51zxw.jpg")
#打开百度页面并截图
driver.get("http://www.baidu.com")
driver.get_screenshot_as_file(r"E:\Python_script\baidu.png")
sleep(2)
driver.quit()