鼠标事件
#鼠标移动至某个元素上面
ele1 = driver.find_element_by_css_selector("a[name='tj_briicon']")
ActionChains(driver).move_to_element(ele1).perform()
sleep(1)
driver.find_element_by_link_text('百科').click()
#鼠标事件右击
ele2 = driver.find_element_by_css_selector("a span[class='title-content-title']")
ActionChains(driver).context_click(ele2).perform()
#鼠标事件双击
ele3 = driver.find_element_by_css_selector("a span[class='title-content-title']")
ActionChains(driver).double_click(ele3).perform()
#拖拽元素到指定位置
sleep(1)
ele4 = driver.find_element_by_css_selector("//*[@id='con-ar']/div/div/div/table/tbody[1]/tr[1]/td[1]/span/a")
target = driver.find_element_by_css_selector('#kw')
ActionChains(driver).drag_and_drop(ele4,target).perform()
#在元素上按下鼠标左键并释放
ele5 = driver.find_element_by_css_selector('#kw')
ActionChains(driver).click_and_hold(ele5).release().perform()
********************************************************************************
键盘事件
在实际的web测试工作中,需要配合键盘来操作,webdriver的Key()类提供键盘上所有的键的操作,还可以模拟组合键CONTROL+a,CONTROL+a/v等
from time import sleep
from case.drivers_set import driver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
driver.get('https://www.baidu.com')
driver.maximize_window()
#单键
driver.find_element_by_css_selector('.s_ipt').send_keys(Keys.SPACE)
#组合键
# 将driver对象的操作权力 让渡给 ActionChains,实现通过键盘或者鼠标对driver对象的操作
action = ActionChains(driver)
driver.find_element_by_css_selector('.s_ipt').send_keys('百度一下')
driver.find_element_by_css_selector('.s_ipt').click()
#CONTROL+a
action.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()
#CONTROL+c
action.key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()
driver.get('https://www.baidu.com')
driver.find_element_by_css_selector('#su').click()
#CONTROL+v
action.key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
driver.quit()
注:1.在使用修饰键时需要key_down()和key_up()方法,修饰键包含ctrl alt shift
2.类似alt+F4 ctrl+alt+delete不能使用