webdriver 提供了很多鼠标操作方法,使用之前需要导入keys类,以下通过实例进一步了解:
#!/usr/bin/python3
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
driver = webdriver.Chrome()
driver.get(‘http://www.baidu.com‘)
driver.find_element_by_css_selector(‘#kw‘).send_keys(‘Python‘)
sleep(2)
#全选
driver.find_element_by_css_selector(‘#kw‘).send_keys(Keys.CONTROL,‘a‘)
sleep(2)
#复制或剪切
#driver.find_element_by_css_selector(‘#kw‘).send_keys(Keys.CONTROL,‘c‘)
driver.find_element_by_css_selector(‘#kw‘).send_keys(Keys.CONTROL,‘x‘)
sleep(3)
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_css_selector(‘#stb‘).click()
sleep(4)
driver.quit()
键盘常用方法;
1 组合键
send_keys(Keys.CONTROL,‘a‘) #全选(Ctrl+A)
send_keys(Keys.CONTROL,‘c‘) #复制(Ctrl+C
send_keys(Keys.CONTROL,‘x‘) #剪切(Ctrl+
send_keys(Keys.CONTROL,‘v‘) #粘贴(Ctrl+V)
2 常用按键
回车键 Keys.ENTER
删除键 Keys.BACK_SPACE
空格键 Keys.SPACE
制表键 Keys.TAB
回退键 Keys.ESCAPE
刷新键 Keys.F5