1.鼠标点击事件用的链接是:http://sahitest.com/demo/clicks.htm(里面可以模拟鼠标点击事件)
selenium鼠标和键盘事件
代码:
from selenium import webdriver
from time import sleep
from selenium.webdriver import ActionChains #鼠标键
from selenium.webdriver.common.keys import Keys # 键盘
# 用这个页面测鼠标点击事件:http://sahitest.com/demo/clicks.htm
from selenium.webdriver.common.keys import Keys
class TestCase(object):
def __init__(self):
self.driver=webdriver.Chrome()
self.driver.maximize_window()
# self.driver.get('http://sahitest.com/demo/clicks.htm')
def test_mouse(self):
self.driver.get('http://sahitest.com/demo/clicks.htm')
"""鼠标事件"""
btn=self.driver.find_element_by_xpath('/html/body/form/input[2]')
# 双击事件
ActionChains(self.driver).double_click(btn).perform() #双击事件
sleep(2)
#单击事件
btn=self.driver.find_element_by_xpath('/html/body/form/input[3]')
ActionChains(self.driver).click(btn).perform()
sleep(2)
#右键点击
btn = self.driver.find_element_by_xpath('/html/body/form/input[4]')
ActionChains(self.driver).context_click(btn).perform()
sleep(4)
def test_key(self):
"""模拟键盘:全选 复制 粘贴"""
self.driver.get('http://www.baidu.com')
kw=self.driver.find_element_by_id('kw')
kw.send_keys('selnium')
#全选
kw.send_keys(Keys.CONTROL,'a')
sleep(2)
#剪切
kw.send_keys(Keys.CONTROL, 'x')
sleep(2)
#粘贴
kw.send_keys(Keys.CONTROL, 'v')
sleep(2)
def test_move(self):
self.driver.get('http://www.baidu.com')
# 鼠标移动到新闻
e=self.driver.find_element_by_link_text("新闻")
ActionChains(self.driver).move_to_element(e).perform()
sleep(2)
e.click()
if __name__ == '__main__':
case=TestCase()
#case.test_mouse()
#case.test_key()
case.test_move()