一、鼠标事件
导入动作链类,动作链可以储存鼠标的动作,并一起执行
from selenium.webdriver import ActionChains
ActionChains(driver)
执行ActionChains中储存的所有动作
perform()
①导入 ActionChains:from selenium.webdriver.common.action_chains import ActionChains
②用于生成模拟用户行为:ActionChains(driver)
③执行存储行为:perform()
④例:ele=driver.find_element_by_link_text(arg)
ActionChains(driver).move_to_element(ele).perform()
context_clik 右击事件
double_clik 双击事件
drag_and_drop 拖动
move_to_element() 鼠标停在一个元素上
click_and_hold 按下鼠标左键在一个元素上
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains import time driver = webdriver.Firefox() driver.get('http://game.61.com/') ele = driver.find_element_by_link_text("游戏") ActionChains(driver).move_to_element(ele).perform() #鼠标停留在“游戏”上 time.sleep(10) sub_ele = driver.find_element_by_link_text('小花仙') sub_ele.click()
一、键盘事件:send_keys()
①导入 Keys:from selenium.webdriver.common.keys import Keys
send_keys(Kyes.BACK_SPACE) 退格键
send_keys(Kyes.CONTAL,'a') 全选
send_keys(Kyes.CONTAL,'v') 粘贴
send_keys(Kyes.CONTAL,'c') 复制
send_keys(Kyes.CONTAL,'x') 剪切
send_keys(Keys.ENTER) 回车
from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Firefox() driver.get('https://www.imooc.com/') ele = driver.find_element_by_class_name("search-input") ele.send_keys('python') ele.send_keys(Keys.ENTER)
完