Office関連

【2018年7月版】SeleniumBasicでMicrosoft Edgeを操作してみました。

2年以上前、SeleniumBasicMicrosoft Edgeに対応した頃、当ブログでも取り上げたことがありました。

SeleniumBasicの更新が2年以上止まっていることもあり、しばらく触っていなかったのですが、気が向いたので久しぶりにSeleniumBasicでEdgeを操作してみることにしました。

SeleniumBasicのインストール

GitHubのReleasesページからファイルをダウンロードし、インストールを行います。2018年7月時点では、SeleniumBasicの最新版はv2.0.9.0となっています。

インストールするWebDriverの選択画面が表示されますが、ここではとりあえずEdge用とIE用のものを選択しておきます。

下図画面の通り、インストール先は「%USERPROFILE%\AppData\Local\SeleniumBasic」となります。

動作確認

インストール後、簡単なコードで動作確認を行います。

Public Sub Sample1()
  With CreateObject("Selenium.EdgeDriver")
    .Start
    .Get "https://www.yahoo.co.jp/"
    .FindElementById("srchtxt").SendKeys "あいうえお"
    MsgBox "一時停止", vbInformation + vbSystemModal
    .Quit
  End With
  MsgBox "終了", vbInformation + vbSystemModal
End Sub

上記コードを実行したところ、下図のエラーが発生しました。
やはり更新の止まっている古いSeleniumBasicではEdgeを操作できないのでしょうか?

確認のため、インストール先のフォルダを見てみると、Edge用のWebDriver(edgedriver.exe)を見つけました。
ただ、このファイルはバージョンが10.0.10240.0と、かなり古いものです。

そこで、公式サイトから最新のファイルをダウンロードし、WebDriverを差し替えたところ(「MicrosoftWebDriver.exe」から「edgedriver.exe」にリネーム)、上記コードが問題なく動作するようになりました。

WebDriverを指定してSeleniumBasicによる操作を実行

WebDriverを最新のファイルに差し替えることで動作することは確認できましたが、ブラウザー(WebDriver)が更新されるたびにファイルを入れ替えるのは、それはそれで面倒くさいものです。

そこで、WebDriverの起動処理を分け、任意の場所にあるWebDriverを実行するようなコードを書いてみました。

Public Sub Sample2()
  Dim pid As Long
  Dim pno As Long: pno = 17556
  
  pid = StartDriver(PortNo:=pno)
  If pid = 0 Then Exit Sub
  With CreateObject("Selenium.WebDriver")
    .StartRemotely "http://localhost:" & pno & "/", "MicrosoftEdge"
    .Get "https://www.yahoo.co.jp/"
    .FindElementById("srchtxt").SendKeys "あいうえお"
    MsgBox "一時停止", vbInformation + vbSystemModal
    .Quit
  End With
  TerminateDriver pid
  MsgBox "終了", vbInformation + vbSystemModal
End Sub

Private Function StartDriver(Optional ByVal BrowserName As String = "MicrosoftEdge", _
                             Optional ByVal PortNo As Long = 58082) As Long
'WebDriver実行
  Dim DriverPath As String
  Dim DriverName As String
  Dim Options As String
  Dim itm As Object, itms As Object
  Dim pid As Long
  Const DriverFolderPath As String = "C:\System\Driver"
  Const EdgeDriverName As String = "MicrosoftWebDriver.exe"
  Const IEDriverName As String = "IEDriverServer.exe"
  
  pid = 0 '初期化
  With CreateObject("Scripting.FileSystemObject")
    Select Case LCase(BrowserName)
      Case "microsoftedge", "edge"
        DriverName = EdgeDriverName
        Options = " --host=localhost --port=" & PortNo
      Case "internet explorer", "ie"
        DriverName = IEDriverName
        Options = " /host=127.0.0.1 /port=" & PortNo & " /log-level=ERROR /silent"
      Case Else: GoTo Fin
    End Select
    DriverPath = .BuildPath(DriverFolderPath, DriverName)
    If .FileExists(DriverPath) = False Then GoTo Fin
  End With
  
  '二重起動確認
  Set itms = CreateObject("WbemScripting.SWbemLocator").ConnectServer.ExecQuery _
             ("Select * From Win32_Process Where Name = '" & DriverName & "'")
  If itms.Count > 0 Then
    For Each itm In itms
      pid = itm.ProcessId: GoTo Fin
    Next
  End If
  
  'WebDriver実行
  With CreateObject("WbemScripting.SWbemLocator").ConnectServer.Get("Win32_Process")
    .Create DriverPath & Options, DriverFolderPath, Null, pid
  End With
  
Fin:
  StartDriver = pid
End Function

Private Sub TerminateDriver(ByVal ProcessId As Long)
'WebDriver終了
  Dim itm As Object, itms As Object
  
  Set itms = CreateObject("WbemScripting.SWbemLocator").ConnectServer.ExecQuery _
             ("Select * From Win32_Process Where ProcessId = " & ProcessId & "")
  If itms.Count > 0 Then
    For Each itm In itms
      itm.Terminate: Exit For
    Next
  End If
End Sub

WebDriverの起動と終了を自力で行うようにしたため、冗長なコードとなっていますが、これであれば好きなフォルダにWebDriverを置いてSeleniumBasicを使うことができます。

VBAマクロからEdgeを操作する機会もなかなか無いと思いますが、興味がありましたら、一度試してみてはいかがでしょうか。


2019/6/5 追記:
Windows 10 May 2019 Updateでも動作確認を行いました。

図形の書式設定ウィンドウ内のコントロールをすべて展開するPowerPointマクロ前のページ

【2018年7月版】ソースコードを番号行付きのテーブルに変換するWordマクロ次のページ

関連記事

  1. Office アドイン

    [Office用アプリ]開発に役立つ資料集

    第一回 Apps for Office 勉強会でも紹介した、Offic…

  2. Office関連

    Excelを使わずにCSVからExcelファイルに変換するPowerShellコード

    CSVファイルからExcelファイルに変換する処理を自動化したい、Ex…

  3. Office関連

    [雑感]Office 365 Soloに向く人、向かない人

    ここ一週間ほどOffice 365 Soloを触ってみて、ある程度のこ…

  4. Windows 10

    [Selenium]ExecuteScriptで指定した要素のIDを取得する。

    MSDN フォーラムにあった質問「Edge向けWebDriverでDO…

  5. Office関連

    RESAS-APIをVBAから呼び出す方法

    下記記事の通り「地域経済分析システム RESAS」のAPIが公開された…

  6. Office関連

    【2017年1月版】Microsoft Edgeを操作するVBAマクロ(DOM編)

    2021/10/1 追記:本記事は公開されてから大分時間が経ってお…

コメント

  • コメント (0)

  • トラックバックは利用できません。

  1. この記事へのコメントはありません。

Time limit is exhausted. Please reload CAPTCHA.

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP