前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >selenium的键盘事件

selenium的键盘事件

作者头像
无涯WuYa
发布2018-10-25 16:20:06
1.8K0
发布2018-10-25 16:20:06
举报

1、 鼠标事件

在测试中,鼠标事件是很常见的,如双击等,鼠标事件需要导入fromselenium.webdriver.common.action_chains import ActionChains

ActionChains类的重要方法通过使用ActionChains类中的执行键盘和鼠标事件,seleniumwebdriver python的API汇总如下:

方法

描述

Example

click(element)

执行点击操作

click(locator)

click_and_hold(element)

向下向左鼠标左键点击一个元素

click_and_hold(locator)

double_click(element)

执行双击操作

double_click(locator)

content_click()

执行右击操作

dontent_click(locator)

move_to_element(element)

鼠标悬停在一个元素

move_to_element(locator)

perform()

执行存储行动

perform()

release(element)

释放按钮

release(locator)

send_keys(element)

发送元素的键包含当前

send_keys('')

send_keys_to_element(element)

发送到元素的element

double_click()

double_click() 是双击一个对象。下面就已双击“百度一下”按钮为实例说明double_click()的使用,在百度搜索输入框输入搜索的关键词,双击”百度一下”按钮,具体见如下的实例代码:

#coding:utf-8

fromselenium import webdriver

fromselenium.webdriver.common.by import By

fromselenium.webdriver.support.ui import WebDriverWait

fromselenium.webdriver.support import expected_conditions

fromselenium.webdriver.common.action_chains import ActionChains

fromselenium.webdriver.common.keys import Keys

importunittest

fromtime import sleep

classdemoTest(unittest.TestCase):

def setUp(self):

self.driver=webdriver.Firefox()

self.driver.maximize_window()

self.driver.implicitly_wait(30)

self.driver.get('http://www.baidu.com')

def testDemo(self):

self.driver.find_element_by_id('kw').send_keys('double_click()')

double=self.driver.find_element_by_id('su')

ActionChains(self.driver).double_click(double).perform()

def tearDown(self):

self.driver.quit()

if__name__=='__main__':

unittest.main(verbosity=2)

move_to_element()

move_to_element()是鼠标悬浮在一个元素上,我们已百度首页为案例来说明它的使用,在百度首页,鼠标悬浮到“更多产品”,就会显示出如下的效果图:

下面,我们结合move_to_element()来实现这样的一个效果,见测试脚本:

#coding:utf-8

fromselenium import webdriver

fromselenium.webdriver.common.by import By

fromselenium.webdriver.support.ui import WebDriverWait

fromselenium.webdriver.support import expected_conditions

fromselenium.webdriver.common.action_chains import ActionChains

from selenium.webdriver.common.keysimport Keys

importunittest

fromtime import sleep

classdemoTest(unittest.TestCase):

def setUp(self):

self.driver=webdriver.Firefox()

self.driver.maximize_window()

self.driver.implicitly_wait(30)

self.driver.get('http://www.baidu.com')

def testDemo(self):

move=self.driver.find_element_by_link_text(u'更多产品')

ActionChains(self.driver).move_to_element(move).perform()

sleep(3)

def tearDown(self):

self.driver.quit()

if__name__=='__main__':

unittest.main(verbosity=2)

click_and_hold()

click_and_hold() 是鼠标左键按在一个元素上,已百度首页搜索为实例,在搜索输入框输入关键词,鼠标按下左键到”百度一下”的按钮,来实现点击的操作,具体见如下的实例代码:

#coding:utf-8

fromselenium import webdriver

fromselenium.webdriver.common.by import By

fromselenium.webdriver.support.ui import WebDriverWait

fromselenium.webdriver.support import expected_conditions

fromselenium.webdriver.common.action_chains import ActionChains

fromselenium.webdriver.common.keys import Keys

importunittest

fromtime import sleep

classdemoTest(unittest.TestCase):

def setUp(self):

self.driver=webdriver.Firefox()

self.driver.maximize_window()

self.driver.implicitly_wait(30)

self.driver.get('http://www.baidu.com')

def testDemo(self):

self.driver.find_element_by_id('kw').send_keys('selenium')

left=self.driver.find_element_by_id('su')

ActionChains(self.driver).click_and_hold(left).perform()

sleep(3)

def tearDown(self):

self.driver.quit()

if__name__=='__main__':

unittest.main(verbosity=2)

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2015-08-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python自动化测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • double_click()
  • move_to_element()
  • click_and_hold()
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档