0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Excel VBA + SeleniumBasic でブラウザ操作

Last updated at Posted at 2020-06-18

環境

windows10
Excel(Office365)
Google Chrome ver 83.0.4103.106

SeleniumBasicのインストール

ChromeDriverのインストール

Chromeのバージョンに合わせたものをインストール
https://chromedriver.storage.googleapis.com/83.0.4103.39/chromedriver_win32.zip

インストールしたフォルダを展開し、chromedriver.exeを、
SeleniumBasicフォルダ(C:\Users[ユーザ名]\AppData\Local\SeleniumBasic)に貼り付け。
(元のchromedriver.exeと入れ替える)
chromedriver.exeを実行。

設定

Excel起動
VBE起動
ツール => 参照設定 => sを押す => Selenium Type Libraryにチェック

Googleを開く

test1.xlsm
Sub test()
    Dim Driver As New Selenium.WebDriver
    Driver.Start "chrome"
    Driver.Get "https://www.google.com/"
    Stop
End Sub

セルに入力された値でgoogle検索

test1.xlsm
Sub search()
    Dim Driver As New Selenium.WebDriver
    Dim searchWord As String
    
    'セルの値を取得
    searchWord = Range("D9").Value
    ' chromeを開く
    Driver.Start "chrome"
    ' googleへアクセス
    Driver.Get "https://www.google.com/"
    ' 検索窓に値を入力
    Driver.FindElementByXPath("//*[@id='tsf']/div[2]/div[1]/div[1]/div/div[2]/input").SendKeys searchWord
    ' 検索ボタンをクリック
    Driver.FindElementByXPath("//*[@id='tsf']/div[2]/div[1]/div[3]/center/input[1]").Click
    Stop
End Sub

Googleを開き、別タブでYahooを開く

test1.xlsm
Sub openGoogleAndYahoo()
    Dim Driver As New Selenium.WebDriver
    Dim ks As New Keys
    
    ' Chromeを開く
    Driver.Start "chrome"
    ' Googlを開く
    Driver.Get "https://www.google.com/"
    
    ' 新しいタブを開く
    Driver.ExecuteScript "window.open()"
    ' タブを切り替える
    Driver.SwitchToNextWindow
    ' Yahooを開く
    Driver.Get "http://www.yahoo.com/"
    Stop
End Sub

参考

VBAのスクレイピングを簡単楽にしてくれるSelenium
https://excel-ubara.com/excelvba4/EXCEL_VBA_401.html

Excelからセレニウム(Seleniumbasic)を使ってブラウザ操作をオートメーション!
https://shigetin.com/20181220/246

0
3
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
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?