1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CDP経由でブラウザを操作するExcel VBAの機能強化版を作成した(WebDriver不要)

1
Last updated at Posted at 2026-03-13

はじめに

本プロジェクトは、企業向けM365 Copilot 5.4 Think Deeperを活用しております。
本記事執筆においても、情報をまとめたりするのに生成AIを活用しています。
また、Githubを使用してのパブリック公開は初めてですので、至らぬ点があるかと思います。ご了承ください。

元のプロジェクトを使用したことがある方向けの内容(初歩的な部分は省略)の記事となっております。

GitHub リンク

リポジトリはこちらです。

背景

@kabkabkab 様の元プロジェクトZeroInstall BrowserDriver for VBAをベースとして、機能強化したものになります。
作者様の元記事はこちら
元のままでも素晴らしいプロジェクトでしたが、個人的に欲しい機能を追加していたり、他のWebスクレイピング用ツールにある機能を追加しているうちに、派生版として公開しても良いくらいの変更内容になったため、Issue・PullRequestとして投げるにはさすがに量が多く、今回公開に踏み切りました。

注意

以下関数について、私が元にしたコードが古く(三年前)、この記事を公開する直前にその事実に気付いたため、急ごしらえで実装しています。
そのため、機能としては再現しているはずですが、コメントの粒度等が他と違います。

QueryString
GetValueFromQueryString
EncodeURIConpornent
GetSecureRandomNumber
GetURLSafeRundomString
GetBase64URLEncodedSHA256
ShowWindow
ScreenShotPasteToSheet

使い方

次のどちらかで利用できます。

  • GitHub 上から src フォルダと Module フォルダを取得し、Excel の VBE にインポートする
  • xlsm フォルダに置いている、インポート済みの .xlsm ファイルをダウンロードして使う

Moduleフォルダについて

Moduleフォルダには SampleModule.basSubModule.bas が入っています

SampleModule

本プロジェクトの機能を試すためのサンプル集です。
RunAllSamples でまとめて実行できるほか、各 Sample_XX_... を個別に実行して、機能ごとの使い方を確認できます。
内容としては、基本操作、要素取得、複数要素取得、属性/プロパティ操作、待機、フレーム/タブ、アラート、ダウンロード、ファイル選択、ネットワーク応答待ち、スクリーンショット/PDF、Cookie / LocalStorage / SessionStorage、Lifecycle Events、CloseTab 系、CSS / Text / Deep 検索、Shadow DOM 検索、table / 配列補助関数、QueryString、公開 Utility API、ShowWindow / ScreenShotPasteToSheet までをカバーしています。

SubModule

補助用の小さな関数をまとめたモジュールです。

  • UTF-8 テキストの保存/読込 (SaveTextUtf8 / LoadTextUtf8)
  • ダウンロード後の別名保存 (DownloadAndSaveAs)
  • フォルダ作成 (EnsureFolderExists)
  • ファイル名抽出 (FileNameFromFullPath)
  • table 要素の二次元配列変換 (TableElementToArray2D)
  • 要素コレクションの text / 属性配列化 (WebElementsToTextArray1D / WebElementsToAttributeArray1D)
  • 要素テキストの連結 (JoinWebElementsText)
  • table の TSV 化 (TableElementToTSV)
  • 二次元配列の Range 出力 (Array2DToRange)

スクレイピング後の整形や保存等で欲しいと思った関数を追加しました。

モジュール別追加関数まとめ

以下は、どのモジュールに(機能的な目線で)どの関数を追加したかを追いたい人向けの一覧です。

a4_ExecuteCDP.cls
  • AcceptAlertDialog
  • AddScriptToEvaluateOnNewDocument
  • DismissAlertDialog
  • DOMStorageClear
  • DOMStorageDisable
  • DOMStorageEnable
  • DOMStorageGetItems
  • DOMStorageRemoveItem
  • DOMStorageSetItem
  • GetResponseBody
  • NetworkDisable
  • NetworkEnable
  • PrintToPDF
  • RemoveScriptToEvaluateOnNewDocument
  • SetDownloadBehavior
  • SetFileInputFilesByBackendNodeId
  • SetInterceptFileChooserDialog
  • SetLifecycleEventsEnabled
  • StorageClearCookies
  • StorageGetCookies
  • StorageGetStorageKey
  • StorageSetCookies
  • WaitForDomMutation
  • WaitForNetworkIdle
IWebDriver.cls
  • AcceptAlertDialog
  • AddBlockedURLPattern
  • AddMockResponse
  • AddScriptToEvaluateOnNewDocument
  • ClearBlockedURLPatterns
  • ClearCookies
  • ClearFileChooserInfo
  • ClearLifecycleEvents
  • ClearMockResponses
  • ClearNetworkResponses
  • CloseTab
  • CloseTabByTitle
  • DisableNetworkInterception
  • DismissAlertDialog
  • DownloadWatchFilePath
  • DownloadWatchStart
  • DownloadWatchSuggestedFilename
  • DownloadWatchWaitCompleted
  • EnableNetworkInterception
  • FileChooserSetFile
  • FindElementByCss
  • FindElementByCssDeep
  • FindElementByText
  • FindElementByTextDeep
  • FindElementsByCss
  • FindElementsByCssDeep
  • FindElementsByText
  • FindElementsByTextDeep
  • GetCookies
  • GetFileChooserBackendNodeId
  • GetFileChooserMode
  • GetLifecycleEvents
  • GetResponseBody
  • GetTabList
  • LocalStorageClear
  • LocalStorageGetAll
  • LocalStorageGetItem
  • LocalStorageRemoveItem
  • LocalStorageSetItem
  • NetworkDisable
  • PrintToPDFSaveAsFile
  • RemoveScriptToEvaluateOnNewDocument
  • SessionStorageClear
  • SessionStorageGetAll
  • SessionStorageGetItem
  • SessionStorageRemoveItem
  • SessionStorageSetItem
  • SetCookie
  • SetCookies
  • SetInterceptFileChooserDialog
  • SetLifecycleEventsEnabled
  • SwitchTabByTitle
  • WaitForDomMutation
  • WaitForNetworkIdle
  • WaitForResponse
IWebElement.cls
  • FindElementByCss
  • FindElementByCssDeep
  • FindElementByText
  • FindElementByTextDeep
  • FindElementsByCss
  • FindElementsByCssDeep
  • FindElementsByText
  • FindElementsByTextDeep
  • FocusAndEnterKey
  • WaitUntilEnabled
  • WaitUntilHidden
  • WaitUntilVisible

機能別関数まとめ

ダイアログ制御
  • AcceptAlertDialog : JavaScript ダイアログを受諾する
  • DismissAlertDialog : JavaScript ダイアログを閉じる
新規ドキュメント時スクリプト制御
  • AddScriptToEvaluateOnNewDocument : 新規ドキュメント時に実行するスクリプトを登録する
  • RemoveScriptToEvaluateOnNewDocument : 登録済みスクリプトを解除する
Cookie / Storage 操作
  • GetCookies : Cookie 一覧を取得する

  • SetCookie : 単一 Cookie を設定する

  • SetCookies : 複数 Cookie をまとめて設定する

  • ClearCookies : Cookie をクリアする

  • StorageGetCookies : CDP の Storage API 経由で Cookie を取得する

  • StorageSetCookies : CDP の Storage API 経由で Cookie を設定する

  • StorageClearCookies : CDP の Storage API 経由で Cookie をクリアする

  • StorageGetStorageKey : Storage 操作用のキーを取得する

  • DOMStorageEnable : DOMStorage 操作を有効化する

  • DOMStorageDisable : DOMStorage 操作を無効化する

  • DOMStorageGetItems : DOMStorage の内容を取得する

  • DOMStorageSetItem : DOMStorage に値を書き込む

  • DOMStorageRemoveItem : DOMStorage の指定キーを削除する

  • DOMStorageClear : DOMStorage をクリアする

  • LocalStorageGetAll : LocalStorage 全体を取得する

  • LocalStorageGetItem : LocalStorage の指定値を取得する

  • LocalStorageSetItem : LocalStorage に値を書き込む

  • LocalStorageRemoveItem : LocalStorage の指定値を削除する

  • LocalStorageClear : LocalStorage をクリアする

  • SessionStorageGetAll : SessionStorage 全体を取得する

  • SessionStorageGetItem : SessionStorage の指定値を取得する

  • SessionStorageSetItem : SessionStorage に値を書き込む

  • SessionStorageRemoveItem : SessionStorage の指定値を削除する

  • SessionStorageClear : SessionStorage をクリアする

Network / Response 制御
  • NetworkEnable : CDP の Network ドメインを有効化する

  • NetworkDisable : Network 監視・取得を無効化する

  • GetResponseBody : 指定レスポンスの本文を取得する

  • EnableNetworkInterception : Network 介入処理を有効化する

  • DisableNetworkInterception : Network 介入処理を無効化する

  • AddBlockedURLPattern : ブロック対象 URL パターンを追加する

  • ClearBlockedURLPatterns : ブロック対象 URL パターンをクリアする

  • AddMockResponse : モックレスポンスを追加する

  • ClearMockResponses : モックレスポンス設定をクリアする

  • ClearNetworkResponses : 保持中のレスポンス情報をクリアする

待機 / イベント監視
  • SetLifecycleEventsEnabled : ライフサイクルイベント通知を有効化/無効化する

  • GetLifecycleEvents : 取得済みライフサイクルイベントを取得する

  • ClearLifecycleEvents : 保持中のライフサイクルイベントをクリアする

  • WaitForNetworkIdle : 通信が落ち着くまで待機する

  • WaitForDomMutation : DOM の変化を待機する

  • WaitForResponse : 条件に一致するレスポンスを待機する

  • WaitUntilEnabled : 要素が操作可能になるまで待機する

  • WaitUntilHidden : 要素が非表示になるまで待機する

  • WaitUntilVisible : 要素が表示されるまで待機する

ダウンロード / ファイル選択
  • SetDownloadBehavior : ダウンロード時の保存動作を設定する

  • FileChooserSetFile : ファイル選択対象にファイルを設定する

  • SetFileInputFilesByBackendNodeId : file input に対してファイルを設定する

  • SetInterceptFileChooserDialog : ファイル選択ダイアログへの介入を設定する

  • ClearFileChooserInfo : ファイル選択関連の保持情報をクリアする

  • GetFileChooserBackendNodeId : ファイル選択対象の backend node id を取得する

  • GetFileChooserMode : ファイル選択モードを取得する

  • DownloadWatchStart : ダウンロード監視を開始する

  • DownloadWatchWaitCompleted : ダウンロード完了まで待機する

  • DownloadWatchFilePath : ダウンロード先ファイルパスを取得する

  • DownloadWatchSuggestedFilename : 提案されたファイル名を取得する

タブ操作
  • GetTabList : タブ一覧を取得する
  • SwitchTabByTitle : タイトルを指定してタブを切り替える
  • CloseTab : 指定タブを閉じる
  • CloseTabByTitle : タイトルを指定してタブを閉じる
要素探索(ページ全体を起点)
  • FindElementByCss : CSS セレクタで単一要素を取得する

  • FindElementsByCss : CSS セレクタで複数要素を取得する

  • FindElementByText : テキスト条件で単一要素を取得する

  • FindElementsByText : テキスト条件で複数要素を取得する

  • FindElementByCssDeep : Shadow DOM を含めて、CSS セレクタで単一要素を取得する

  • FindElementsByCssDeep : Shadow DOM を含めて、CSS セレクタで複数要素を取得する

  • FindElementByTextDeep : Shadow DOM を含めて、テキスト条件で単一要素を取得する

  • FindElementsByTextDeep : Shadow DOM を含めて、テキスト条件で複数要素を取得する

要素操作補助
  • FocusAndEnterKey : 対象要素へフォーカスした後に Enter キー操作を行う
出力
  • PrintToPDF : ページを PDF として出力する
  • PrintToPDFSaveAsFile : ページを PDF にしてファイル保存する

最後に

質問・ご意見等ございましたらコメントまでお願いします。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?