Selenium - 模拟键盘鼠标操作
作者:互联网
Selenium - 模拟键盘鼠标
鼠标操作
-
当出现由 Selenium 的
click
无法进行操作,或者需要对元素进行拖拽等操作时,可以采用鼠标模拟人工操作:from selenium import webdriver # 导入鼠标库 from selenium.webdriver.common.action_chains import ActionChains driver = webdriver.Chrome() driver.get('http://www.baidu.com') # 实例化鼠标库,并引用webdeiver mouse = ActionChains(driver) element = driver.find_element_by_id('kw') # 将鼠标悬停到元素上 mouse.move_to_element(element).perform() # 鼠标左键点击 mouse.click(element) # 鼠标右键点击 mouse.context_click(element) # 鼠标左键点击,不松开 mouse.click_and_hold(element) # 鼠标左键双击 mouse.double_click(element) # 拖拽到某个元素然后松开 mouse.drag_and_drop(source, target) # 拖拽到某个坐标然后松开 mouse.drag_and_drop_by_offset(source, xoffset, yoffset)
键盘操作
-
常见的 Selenium 模拟键盘主要是针对一个快捷键的操作,例如复制、粘贴;
from selenium import webdriver # 导入selenium 的键盘模块 from selenium.webdriver.common.keys import Keys driver = Webdriver.Chrome() # 键盘操作 driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'c') # 复制 driver.find_element_by_id('kw').send_keys(Keys.SHIFT, '1') # 中文叹号 driver.find_element_by_id('kw').send_keys(Keys.DELETE) # 删除按钮 driver.find_element_by_id('kw').send_keys(Keys.TAB) # 制表键TAB driver.find_element_by_id('kw').send_keys(Keys.ENTER) # 回车按钮ENTER driver.find_element_by_id('kw').send_keys(Keys.F1) # 键盘F1按钮
标签:鼠标,Keys,Selenium,driver,element,键盘,mouse,id 来源: https://www.cnblogs.com/mzline/p/16697676.html