在webdriver中这些关于鼠标操作的方法由ActionChains类提供。
ActionChains类提供的鼠标操作的常用方法:l Perform() 执行所有ActionChains()中存储的行为
l Context_click() 右击
l Double_click() 双击
l Drag_and_drop() 拖动
l Move_to_element() 鼠标悬停
#conding=utf-8
From seleniumimport webdriver
Fromselenium.webdriver.common.action_chains import ActionChains
Driver=webdrive.Chrome()
Driver.get(“http://yun.baidu.com/”)
Driver.find_element_by_id(“TANGRAM__PSP_4__userName”).clear()
Driver.find_element_by_id(“TANGRAM__PSP_4__userName”).send_keys(“username”)
Driver.find_element_by_id(“TANGRAM__PSP_4__password”).clear()
Driver.find_element_by_id(“TANGRAM__PSP_4__password”).send_keys(“password”)
Driver.find_element_by_id(“TANGRAM__PSP_4__submit”).click()
Disk_NET=driver.find_element_by_class_name(“pulldown-nav”)
ActionChains(driver).move_to_element(Disk_NET)
DC=driver.find_element_by_class_name(“li disk”)
ActionChains(driver).double_click(DC)Drag_and_drop(source,target):在原元素上按下鼠标左键,然后移动到目标元素上释放
4.5 键盘事件Keys类提供键盘上几乎所有的按键的方法,send_keys()方法可以模拟键盘输入,除此之外还可以模拟键盘上的一些组合键,例如Ctrl+A,Ctrl+C等
KEYS.py#coding=utf-8
From selenium import webdriver
From selenium.webdriver.commom.keys import Keys
Driver=webdriver.Chrome()
Driver.get(“http://www.baidu.com”)
Driver.find_element_by_id(“kw”).send_keys(“seleniumm”)
#删除多输入的一个m
Driver.find_element_by_id(“kw”).send_keys(Keys.BACK_SPACE)
#Ctrl+a全选输入框内容
Driver.find_element_by_id(“kw”).send_keys(Keys.CONTROL,’a’)
#Ctrl+x全选输入框内容
Driver.find_element_by_id(“kw”).send_keys(Keys.CONTROL,’x’)
#Ctrl+v全选输入框内容
Driver.find_element_by_id(“kw”).send_keys(Keys.CONTROL,’v’)
Driver.find_element_by_id(“su”).send_keys(Keys.ENTER)
导入键盘类包:From selenium.webdriver.commom.keys import Keys
send_keys(Keys.BACK_SPACE)删除键(BackSpace)
send_keys(Keys.SPACE)空格键(Space)
send_keys(Keys.TAB)制表键(Tab)
send_keys(Keys.ESCAPE)回退键(Esc)
send_keys(Keys.ENTER)回车键(Enter)
send_keys(Keys.CONTROL,'a')全选(Ctrl+A)
send_keys(Keys.CONTROL,'c')复制(Ctrl+C)
send_keys(Keys.CONTROL,'x')剪切(Ctrl+X)
send_keys(Keys.CONTROL,'v')粘贴(Ctrl+V)
send_keys(Keys.F1)键盘F1欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |