AutoIt WebDriver UDFを使ってChrome操作 キーボード操作

webdriver sendkeys WebDriver

AutoItではSelenium BasicでもChromeなどのブラウザを操作することが出来ますが、別途Selenium Basicをインストールする必要があり、なかなか気軽ではないので、少々面倒ではありますがWebDriver UDFを自分は使っています。今回はその中でもキーボード操作について記載します。SetKeys()がないので作って矢印やSHIFTキーを渡せるようにします。

スポンサーリンク

1. _WD_ElementAction()では出来ない

フォームのinput要素に対して文字入力は_WD_ElementAction($sess,$elm,”value”,”書きたいこと”)ですね。
Enterもinput要素になら\nを書けばOKです。例えばgoogleの検索inputにキーワード書いてEnter押す操作は、_WD_ElementAction($sess,$elm,”value”,”autoit rpa\n”)です。

2. ShiftやDelete,矢印などはWebDriver UDFにないから自作の必要

SeleniumだとSendKeys(Key.LeftArrow)とかで実現できるのをWebDriver UDFで行うにはwdcore.au3の_WD_Action()にで行いますが複雑です。w3cの仕様を見てみますが、具体的にどうすればいいの?という感じですね。

WebDriver
WebDriver is a remote control interface that enables introspection and control of user agents. It provides a platform- and language-neutral wire protocol as a w...

2.1 WebDriver UDFのdemoを見るとポインター移動デモがあります。

走らせてみましょう。これはマウスポインタのデモですね。wd_demo.au3のDemoActions()関数を見てみます。複雑ですね。要はactionsにkeydown,keyup,mouseDown,mouseMove,mouseUpなど1つ1つの動作を書いてactionsとして送る必要があるようです。

Func DemoActions()
  Local $sElement, $aElements, $sValue

  _WD_Navigate($sSession, "http://google.com")
  $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $sElementSelector)

ConsoleWrite("$sElement = " & $sElement & @CRLF)

  $sAction = '{"actions":[{"id":"default mouse","type":"pointer","parameters":{"pointerType":"mouse"},"actions":[{"duration":100,"x":0,"y":0,"type":"pointerMove","origin":{"ELEMENT":"'
  $sAction &= $sElement & '","' & $_WD_ELEMENT_ID & '":"' & $sElement & '"}},{"button":2,"type":"pointerDown"},{"button":2,"type":"pointerUp"}]}]}'

ConsoleWrite("$sAction = " & $sAction & @CRLF)

  _WD_Action($sSession, "actions", $sAction)
  sleep(5000)
  _WD_Action($sSession, "actions")
  sleep(5000)
EndFunc

3. _WD_Actionでkeydown,keyupがキー操作

そうは言っても難しいのでググってみるとサンプルありました。

WebDriver UDF - Help & Support

キーはw3cで記載されているコードを使います。

WebDriver
WebDriver is a remote control interface that enables introspection and control of user agents. It provides a platform- and language-neutral wire protocol as a w...
;; 2回ページダウンをしてスクロールする
$sAction = '{"actions":[{"type": "key", "id": "keyboard_1", "actions": [{"type": "keyDown", "value": "\uE00F"}, {"type": "keyUp", "value": "\uE00F"}]}]}'
_WD_Action($sess, "actions", $sAction)
Sleep(500)

$sAction = '{"actions":[{"type": "key", "id": "keyboard_1", "actions": [{"type": "keyDown", "value": "\uE00F"}, {"type": "keyUp", "value": "\uE00F"}]}]}'
_WD_Action($sess, "actions", $sAction)

自分は、キーコードはSelenium pythonやbasicの事例に沿ってGlobal const Keys_RETURN = “\uE008″とかにして定義、wd_core.au3の最初に#include “wd_keycode.au3″追加。

Global Const $Keys_CANCEL = "\uE001"
Global Const $Keys_HELP = "\uE002"
Global Const $Keys_BACKSPACE = "\uE003"
Global Const $Keys_TAB = "\uE004"
Global Const $Keys_CLEAR = "\uE005"
Global Const $Keys_RETURN = "\uE006"
Global Const $Keys_ENTER = "\uE007"
Global Const $Keys_SHIFT = "\uE008"
Global Const $Keys_CONTROL = "\uE009"
Global Const $Keys_ALT = "\uE00A"
Global Const $Keys_PAUSE = "\uE00B"
Global Const $Keys_ESCAPE = "\uE00C"
Global Const $Keys_SPACE = "\uE00D"
Global Const $Keys_PAGEUP = "\uE00E"
Global Const $Keys_PAGEDOWN = "\uE00F"
send_keys(special)-Python
「Selenium Python」の特殊キーを入力をする「send_keys(special)」の使い方を解説します

普段、ChromeではCtrl+L(アドレスバー)、キーワード追加でEnter、スペースキーでページダウン、SHIFT+スペースでページアップとかするので、それが出来る関数作ります。
wd_core.au3にでも追加しておくと良いです。

Func _WD_SendKeys($sess, $keycode, $str2=False)
  $sAction = '{"actions":[{"type": "key", "id": "keyboard_1", "actions": ['
  $sAction = $sAction & '{"type":"keyDown","value":"'&$keycode&'"},'

  If $str Then 
	Local $num = StringLen($str)
	For $i=1 To $num Step 1 
  	  Local $schar = StringMid($str, $i,1)
	  $sAction = $sAction & '{"type": "keyDown", "value": "'&$schar&'"}, {"type": "keyUp", "value": "'&$schar&'"},'
	Next
  EndIf

  $sAction = $sAction & '{"type":"keyUp","value":"'&$keycode&'"}]}]}'

  _WD_Action($sess, "actions", $sAction)
EndFunc

3.1 キーボードIDはid:keyboard_1で良いようです

ここでid:keyboard_1って何?って思うんですが、これでいいみたいです。
npmのactionでもkeyboard_1と指定しているので。

webdriver-actions
Backwards-compatible W3C WebDriver Actions API bindings Edit. Latest version: 0.1.4, last published: 6 years ago. Start using webdriver-actions in your project ...

4. Ctrlキーが送られない

自分がテストした時はCtrlキーが送られませんでした・・・左Ctrl,右Ctrlキーどちらもです。
Chrome WebDriver自体がまだ対応していないのかは不明ですが。
まぁCtrl+のショートカットはちょっと保留にしておきます。

あと気づいたのですが、SHIFT+スペースで上にスクロールもよく使いますが、今回作成したSendKeys関数だと実現できませんね・・・これも保留で汗

9. 参考リンク

Ctrl not pressed for Ctrl-A (SendKeys of Actions API) · Issue #665 · mozilla/geckodriver
Firefox Version 53.0 (64bit) geckodriver 0.15 Platform Windows 10 Anniversary Update (Redstone 1) Steps to reproduce - When sending "Ctrl-A" to Actions.sendKeys...
Sending keyboard shortcuts to chrome with Selenium and Ruby
I'm attempting to launch the devtools in a chrome browser on Linux by using the keyboard shortcuts. Because I'm using Ruby and it does not have a chord method,...

コメント

タイトルとURLをコピーしました