《SeleniumBasic 3.141.0.0 - 在VBA中操作浏览器》系列文章之七:使用行为链模拟鼠标和键盘操作

SeleniumBasic中的Actions类可以实现鼠标和键盘操作。方法列表如下

其中标记为橙色的是键盘方面的操作。标记绿色的Create方法是创建行为时必须要运行的。

  • Function Click([onElement As IWebElement]) As Actions
  • Function ClickAndHold([onElement As IWebElement]) As Actions
  • Function ContextClick([onElement As IWebElement]) As Actions
  • Sub Create(driver As IWebDriver)
  • Function DoubleClick([onElement As IWebElement]) As Actions
  • Function DragAndDrop(source As IWebElement, target As IWebElement) As Actions
  • Function DragAndDropToOffset(source As IWebElement, offsetX As Long, offsety As Long) As Actions
  • Function KeyDown(theKey As String, [onElement As IWebElement]) As Actions
  • Function KeyUp(theKey As String, [onElement As IWebElement]) As Actions
  • Function MoveByOffset(offsetX As Long, offsety As Long) As Actions
  • Function MoveToElement(toElement As IWebElement, [offsetX As Long], [offsety As Long]) As Actions
  • Sub Perform()
  • Function Release_([onElement As IWebElement]) As Actions
  • Function Sendkeys(keysToSend As String, [element As IWebElement]) As Actions

执行行为链时必须运行Perform方法,否则没有效果。

可以看到均有一个element可选参数,这个可以提供,也可以不写。

下面的程序,演示了在搜索中按下右键,弹出快捷菜单。

    Dim Action As SeleniumBasic.Actions
    Set Action = New SeleniumBasic.Actions
    Action.Create driver:=WD
    Action.ContextClick(onElement:=keyword).Perform

WD是浏览器对象,通过Action.Create方法与之关联。运行ContextClick方法,会在元素上按下右键。对于单击、双击都是相同的用法,不需举例。

按键方面,可以使用KeyDown按下Control、Shift或功能键,也可以使用KeyUp弹起。

还可以使用SendKeys发送字符串。下面程序演示了创建行为链,向百度搜索框中发送Control+A全选,然后发送Control+X剪切。这样就把文本框的内容剪切到剪贴板上了。

    Dim Key As SeleniumBasic.Keys
    Dim Action As SeleniumBasic.Actions
    Set Key = New SeleniumBasic.Keys
    Set Action = New SeleniumBasic.Actions
    Action.Create driver:=WD
    Call Action.KeyDown(Key.Control, keyword).SendKeys("a", keyword).KeyUp(Key.Control, keyword).KeyDown(Key.Control, keyword).SendKeys("x", keyword).KeyUp(Key.Control, keyword).Perform

 注意,KeyDown和KeyUp后面跟的参数,需要从Keys类中得到。支持的按键有

Property Add As String
Property Alt As String
Property ArrowDown As String
Property ArrowLeft As String
Property ArrowRight As String
Property ArrowUp As String
Property Backspace As String
Property Cancel As String
Property Clear As String
Property Command As String
Property Control As String
Property Decimal As String
Property Delete As String
Property Divide As String
Property Down As String
Property End As String
Property Enter As String
Property Equal As String
Property Escape As String
Property F1 As String
Property F10 As String
Property F11 As String
Property F12 As String
Property F2 As String
Property F3 As String
Property F4 As String
Property F5 As String
Property F6 As String
Property F7 As String
Property F8 As String
Property F9 As String
Property Help As String
Property Home As String
Property Insert As String
Property Left As String
Property LeftAlt As String
Property LeftControl As String
Property LeftShift As String
Property Meta As String
Property Multiply As String
Property Null As String
Property numberpad0 As String
Property numberpad1 As String
Property numberpad2 As String
Property numberpad3 As String
Property numberpad4 As String
Property numberpad5 As String
Property numberpad6 As String
Property numberpad7 As String
Property numberpad8 As String
Property numberpad9 As String
Property PageDown As String
Property PageUp As String
Property Pause As String
Property Return As String
Property Right As String
Property Semicolon As String
Property Separator As String
Property Shift As String
Property Space As String
Property Subtract As String
Property Tab As String
Property Up As String

posted @ 2020-09-19 11:13  ryueifu  阅读(4253)  评论(1编辑  收藏  举报