Windows Excel VBA + Selenium WEBスクレイピング chrome edge

Selenium を導入し、VBA でchromeやedgeを自動操作するようなRPAのような物を作る事できる。まずは設定手順となります。下記サイト等を参考にSelenium+VBA試してみました。その他、pythonを使って同様な事ができます。
Selenium Basic のインストール
http://florentbr.github.io/SeleniumBasic/
Download/Release page をクリック
SeleniumBasic-xxx.exe をダウンロード、画面の指示に従いインストールします。
chrome 最新化
ChromeDriverを最新版のものに更新する。以下のサイトより入手
ChromeDriverのバージョンは、現在使用しているchromeのバージョンと合わせます。バージョン確認方法は下記。上3桁までが一致していればOK


現在は115がダウンロード不可であるが、同一バージョンをダウンロードしてください。

ダウンロードした、chromedriver-win32.zip を解凍

Selenium Basic をインストールしたフォルダを開く
C:\Users\ユーザー名\AppData\Local\SeleniumBasic
配下に chromedriver.exe ファイルを貼り付け。edgeも同様

.NET Framework のインストール
インストールの方法は、
C:\Users\ユーザー名\AppData\Local\SeleniumBasic\Scripts
StartChrome.vbs をダブルクリック
ユーザープロファイルの設定
ユーザープロファイルの利用
VBEの参照設定を実施
次に、参照設定を実施します。VBE(VisualBasicEditor) を開いて、ツール > 参照設定を選択
Selenium Type Libraryにチェック


サンプルコードです。実行してみます。

実行結果。chromeが自動起動して、キーワード入力→検索→結果一覧と自動で実行できた。
まるでRPAソフトを使って自動操作しているようです。

下記がサンプルコードです。
Sub Sample()
Dim Driver As Selenium.ChromeDriver
Dim By As New Selenium.By
Dim element As Selenium.WebElement
Set Driver = New Selenium.ChromeDriver
With Driver
'---Chromeを立ち上げる
.Start
'---Google検索へ移動
.Get "https://www.google.com/"
'---検索ウィンドに"ジャベ雄"を入力
.FindElement(By.Css("textarea[type=search]")).SendKeys "アテモヤ"
.Wait 1000
'---Google 検索ボタンを押下
.FindElement(By.Css("input[value=""Google 検索""]")).Click
.Wait 1000
'---検索結果の中からこのサイト("https://javeo.jp/"の前方一致だったらタイトルリンクをクリック
For Each element In .FindElements(By.Css("#search > div > div"))
If element.FindElement(By.Css("div > div > div > div > a")).IsDisplayed Then
If element.FindElement(By.Css("div > div > div > div > a")).Attribute("href") Like "https://javeo.jp/*" Then
Call element.FindElement(By.Css("div > div > div > div > a")).Click
.Wait 1000
Exit For
End If
End If
Next
'---Chromeを閉じる
.Close
End With
Set Driver = Nothing
End Sub
-
前の記事
初の伊江島 家族皆んで芳魂之塔へ 2023.08.03
-
次の記事
Laravel フレームワーク初期導入の手順の覚書 便利なlaravel-adminを導入してみる 2023.08.21