宝くじと過ごす日々

Excel,VBA,Radio,モキュメンタリー好き事務の活動記録です。

VBA×Selenium×Chromeの基本的な使い方

VBA×Selenium×Chromeの基本的な使い方

Chrome操作において、基本的に下記だけ覚えておけばとりあえず一通りやりたいことはできるはずです。

環境のセッティングは各自調べて用意してください。

●Chromeを使う宣言

Dim Driver As NewSelenium.ChromeDriver

●Chromeをシークレットモードで使用したいとき

Driver.AddArgument "--incognito"

●Chromeのブラウザを最大化したいとき

Driver.AddArgument "disable-gpu"

Driver.AddArgument "start-maximized"

●Chromeのブラウザを非表示/バックグラウンドで起動したいとき

Driver.AddArgument "headless"

●Chromeの起動

Call Driver.Start("chrome")

●VBA×Seleniumで任意のURLにアクセスしたいとき

Driver.Get https://www.google.com/

●VBA×Seleniumで任意のテキストボックスに文字を入力したいとき

Driver.FindElementByXPath("任意のxPath").SendKeys 任意の文字列

※xPathにダブルクォーテーションがある場合は、さらに"をその両端につけないとエラーになるので注意してください。

●VBA×Seleniumでブラウザ上のボタンをクリックしたいとき

Driver.FindElementByXPath("任意のxPath").Click

Driver.FindElementByXPath("任意のxPath").Submit

●VBA×Seleniumで処理を一時的に止めたいとき(1000=1秒)

Driver.Wait 500

●VBA×Seleniumで要素の数だけ繰り返し処理したいとき

For i = 1 To Driver.FindElementsByXPath("任意のxPath").Count

~繰り返したい処理~

Next

●VBA×Seleniumでブラウザ上の任意のテキストをワークシートに出力したいとき

Range(A1) = Driver.FindElementByXPath("任意のxPath").Text

●VBA×Seleniumでブラウザ上の複数の任意のテキストを順にワークシートに出力したいとき

For i = 1 To Driver.FindElementsByXPath("任意のxPath").Count

Cells(i , 1) = Driver.FindElementsByXPath("任意のxPath")(i).Text

Next i

f:id:osushidaisukikun:20210824160755j:image