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