vb.net中datagridview的鼠标事件_selenium自动化测试中的鼠标事件

在做自动化测试的时候,经常会遇到这种情况,某个页面元素,你必须要把鼠标移动到上面才能显示出元素。那么这种情况,我们怎么处理呢?

selenium给我们提供了一个类来处理这类事件——ActionChains。

ActionChains可以对需要模拟鼠标操作才能进行的情况,比如单击、双击、点击鼠标右键、拖拽等等进行操作。

使用鼠标事件之前需要要导入ActionChains类,导入方法如下:

  1. from selenium.webdriver.common.action_chains import ActionChains

复制代码

那么我们通过一个场景来看下鼠标事件的应用范围吧,如我们现在要去操作下快捷导航中的某个控件(搜索板块控件)

599b8a25100e1373a35aab8a970157d0.png

首先我们得先定位到快捷导航控件,然后通过鼠标事件讲鼠标挪动到快捷导航快捷,那么我们先看下鼠标事件的方法:

1、move_to_element

  1. def move_to_element(self, to_element):
  2. """
  3. Moving the mouse to the middle of an element.
  4. :Args:
  5. - to_element: The WebElement to move to.
  6. """
  7. if self._driver.w3c:
  8. self.w3c_actions.pointer_action.move_to(to_element)
  9. self.w3c_actions.key_action.pause()
  10. else:
  11. self._actions.append(lambda: self._driver.execute(
  12. Command.MOVE_TO, {'element': to_element.id}))
  13. return self

复制代码

其实看源码就会发现方法很简单,需要传入的参数就是鼠标要挪动到的控件上,那么代码如何实现:

  1. from selenium import webdriver
  2. import time
  3. from selenium.webdriver.common.action_chains import ActionChains
  4. driver = webdriver.Chrome()
  5. driver.get('http://www.bcbxhome.com')
  6. driver.maximize_window()
  7. time.sleep(1)
  8. ele = driver.find_element_by_id('qmenu') ###定位到的快捷导航控件
  9. time.sleep(2)
  10. ActionChains(driver).move_to_element(ele).perform()

复制代码

2、move_to_element_with_offset

  1. def move_to_element_with_offset(self, to_element, xoffset, yoffset):
  2. """
  3. Move the mouse by an offset of the specified element.
  4. Offsets are relative to the top-left corner of the element.
  5. :Args:
  6. - to_element: The WebElement to move to.
  7. - xoffset: X offset to move to.
  8. - yoffset: Y offset to move to.
  9. """
  10. if self._driver.w3c:
  11. self.w3c_actions.pointer_action.move_to(to_element, xoffset, yoffset)
  12. self.w3c_actions.key_action.pause()
  13. else:
  14. self._actions.append(
  15. lambda: self._driver.execute(Command.MOVE_TO, {
  16. 'element': to_element.id,
  17. 'xoffset': int(xoffset),
  18. 'yoffset': int(yoffset)}))
  19. return self

复制代码

看到源码我们就知道需要传入三个参数,第一参数就是鼠标从哪里看是挪到,是从这个控件的左上角,偏移量是相对这个左上角进行偏移的,偏移x和y方向

  1. from selenium import webdriver
  2. import time
  3. from selenium.webdriver.common.action_chains import ActionChains
  4. driver = webdriver.Chrome()
  5. driver.get('http://www.bcbxhome.com')
  6. driver.maximize_window()
  7. time.sleep(1)
  8. ele = driver.find_element_by_id('qmenu')
  9. time.sleep(2)
  10. ActionChains(driver).move_to_element_with_offset(ele,10,10).perform()

复制代码

3、click_and_hold(on_element=None) ——点击鼠标左键,不松开

4、context_click(on_element=None) ——点击鼠标右键

5、double_click(on_element=None) ——双击鼠标左键

6、drag_and_drop(source, target) ——拖拽到某个元素然后松开

7、drag_and_drop_by_offset(source, xoffset, yoffset) ——拖拽到某个坐标然后松开

8、key_down(value, element=None) ——按下某个键盘上的键

9、key_up(value, element=None) ——松开某个键

10、move_by_offset(xoffset, yoffset) ——鼠标从当前位置移动到某个坐标

vb.net操作DataGridView控件的用法的集合,包括: 1. DataGridView当前的单元格属性取得、变更 2. DataGridView编辑属性 3. DataGridView最下面一列新追加行非表示 4. DataGridView判断当前选行是否为新追加的行 5. DataGridView删除行可否设定 6. DataGridView行列不表示和删除 DataGridView控件用法合集(二) 7. DataGridView行列宽度高度设置为不能编辑 8. DataGridView行高列幅自动调整 9. DataGridView指定行列冻结 10. DataGridView列顺序变更可否设定 11. DataGridView行复数选择 12. DataGridView选择的行、列、单元格取得 DataGridView控件用法合集() 13. DataGridView指定单元格是否表示 14. DataGridView表头部单元格取得 15. DataGridView表头部单元格文字列设定 16. DataGridView选择的部分拷贝至剪贴板 17.DataGridView粘贴 18. DataGridView单元格上ToolTip表示设定(鼠标移动到相应单元格上时,弹出说明信息) DataGridView控件用法合集(四) 19. DataGridView的ContextMenuStrip属性 20. DataGridView指定滚动框位置 21. DataGridView手动追加列 22. DataGridView全体分界线样式设置 23. DataGridView根据单元格属性更改显示内容 24. DataGridView新追加行的行高样式设置る 25. DataGridView新追加行单元格默认值设置 DataGridView输入错误数据的处理(五) 26. DataGridView单元格数据错误标签表示 27. DataGridView单元格内输入值正确性判断 28. DataGridView单元格输入错误值事件的捕获 DataGridView控件用法合集(六) 29. DataGridView行排序(点击列表头自动排序的设置) 30. DataGridView自动行排序(新追加值也会自动排序) 31. DataGridView自动行排序禁止情况下的排序 32. DataGridView指定列指定排序 DataGridView控件用法合集(七) 33. DataGridView单元格样式设置 34. DataGridView文字表示位置的设定 35. DataGridView单元格内文字列换行 36. DataGridView单元格DBNull值表示的设定 37. DataGridView单元格样式格式化 38. DataGridView指定单元格颜色设定 39. DataGridView单元格文字字体设置 40. DataGridView根据单元格值设定单元格样式 DataGridView控件用法合集(八) 41. DataGridView设置单元格背景颜色 42. DataGridView行样式描画 43. DataGridView显示行号 44. DataGridView焦点所在单元格焦点框不显示的设定 DataGridView控件用法合集(九) 45. DataGridView显示选择框CheckBox 46. DataGridView显示下拉框ComboBox 47. DataGridView单击打开下拉框 48. DataGridView显示按钮 49. DataGridView显示链接 50. DataGridView显示图像 DataGridView控件用法合集(十) 51. DataGridView编辑单元格控件取得 52. DataGridView输入自动完成 53. DataGridView单元格编辑时键盘KEY事件取得 54. DataGridView下拉框(ComboBox)单元格编辑时事件取得 55. DataGridView下拉框(ComboBox)单元格允许文字输入设定 DataGridView控件用法合集(十一) 56. DataGridView根据值不同在另一列显示相应图片 57. DataGridView显示进度条(ProgressBar) 58. DataGridView添加MaskedTextBox DataGridView控件用法合集(十二) 59. DataGridViewEnter键按下焦点移至旁边的单元格 60. DataGridView行集合化(Group)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值