↓のサイトで、IEを表示、要素を抜き出すことができました。
次は、ボタンをすべて確認し、任意のボタンをクリックする方法を説明します。


VBAでIEのボタンをクリックする方法~VBA(マクロ)でIE操作
今回は、VBAでIEのボタンをクリックする方法を紹介したいと思います。
まず、サイト内のインプット要素の一覧を見てみましょう。
下記コードは「グーグルの検索サイト」の全てのインプット要素をエクセルに書き出すコードです。
ダブルクリックでコピーできますのでとりあえずVBAに張り付けて実行してみてください。
Sub IEinput()
Application.ScreenUpdating = False
'IEの起動
Dim objIE As Object
Set objIE = GetObject("", "InternetExplorer.Application")
objIE.Visible = True
objIE.Navigate "https://www.google.com/?hl=ja" ' このURLを任意に変更
' ページの表示完了待ち。
While objIE.ReadyState <> 4 Or objIE.Busy = True
DoEvents
Wend
On Error Resume Next '値がないとエラーが出るので、エラー回避用
I = 1 '開始行を指定
J = objIE.document.all.Length '要素の数を知る
Cells(I, 1).Value = "No"
Cells(I, 2).Value = "tagname"
Cells(I, 3).Value = "Type"
Cells(I, 4).Value = "NAME"
Cells(I, 5).Value = "ID"
Cells(I, 6).Value = "className"
Cells(I, 7).Value = "TABINDEX"
Cells(I, 8).Value = "Vakue"
Cells(I, 9).Value = "checked"
Cells(I, 10).Value = "親のtagname"
Cells(I, 11).Value = "innertext"
Cells(I, 12).Value = "outertext"
Cells(I, 13).Value = "outherhtml"
Cells(I, 14).Value = "innerhtml"
Cells(I, 15).Value = "リンク先"
Dim A As Object
For Each A In objIE.document.getElementsByTagName("INPUT")
Cells(I + 1, 1) = I - 1 '個数
Cells(I + 1, 2) = A.TAGNAME 'TAGネーム
Cells(I + 1, 3) = A.Type 'タイプ ※selectボックスは”select-one”と取得
Cells(I + 1, 4) = A.Name '名前
Cells(I + 1, 5) = A.ID 'ID
Cells(I + 1, 6) = A.className 'クラス名
Cells(I + 1, 7) = A.TabIndex 'フォーカス順序 (Tabでの移動順)
Cells(I + 1, 8) = A.Value '値
Cells(I + 1, 9) = A.Checked 'チェック状態 (True = チェック有り、false = チェック無し)
'checkboxやradioボタンから取得します
Cells(I + 1, 10) = A.parentElement.TAGNAME '親のTAG
If Len(A.innerHTML) > 50 Then
Cells(I + 1, 11) = Left(A.innertext, 10) & " ~~~ " & Right(A.innertext, 10)
Cells(I + 1, 12) = Left(A.outertext, 10) & " ~~~ " & Right(A.outerrext, 10)
Cells(I + 1, 13) = Left(A.outerHTML, 10) & " ~~~ " & Right(A.outerHTML, 10)
Cells(I + 1, 14) = Left(A.innerHTML, 10) & " ~~~ " & Right(A.innerHTML, 10)
Else
Cells(I + 1, 11) = A.innertext
Cells(I + 1, 12) = A.outertext
Cells(I + 1, 13) = A.outerHTML
Cells(I + 1, 14) = A.innerHTML
End If
I = I + 1
'ステータスバーに進捗を表示
Application.StatusBar = I & "/" & J
Next
On Error GoTo 0
Cells.WrapText = False
Application.ScreenUpdating = True
Application.StatusBar = False
Range(Cells(1, 1), Cells(1, 14)).EntireColumn.AutoFit
Range(Cells(1, 2), Cells(1, 2)).EntireColumn.Interior.ColorIndex = 6
Range(Cells(1, 4), Cells(1, 4)).EntireColumn.Interior.ColorIndex = 6
Range(Cells(1, 8), Cells(1, 8)).EntireColumn.Interior.ColorIndex = 6
End Sub
簡単にプログラム説明
ボタンのTagNameは「INPUT」 TYPE「submit」 か「BUTTON」
ボタンの TagName は「INPUT」か「BUTTON」ですが、
googleでは「INPUT」を用いているようですので、
サイトを表示させた後、
For Each A In objIE.document.getElementsByTagName(“INPUT“) ~~~ Next
で「INPUT」の要素をすべて検索し、
抜き出した要素からデータをエクセルに書き出していっています。
これで、 TagNameがINPUTの一覧表ができたと思います。
その中でも今回は「Google 検索」ボタンをクリックしたいので、
Type(c列)がsubmit、valueが「Google 検索」 のものを探します。
結果、No4が探している「Google 検索」ボタンになり、
ボタンのNameが"btnK"であることが分かりました。

ボタンをクリックする。
全てのinputを抜き出し、入力したい要素を見つけることができました。
次はこの要素の内容を用いて、今度はボタンをクリックしていきたいと思います。
やることは簡単、先ほどと同じ要領で、
①FOR EACH でINPUTをすべて抜き出す。
②IF文で入力したい要素を見つける。→今回は.Nameが"btnk" or Valueが”Google 検索”
③.clickでボタンを押す
となっております。
ダブルクリックでコピーできますのでVBAに張り付けて実行してみてください。
Sub IE_buttonCLICK()
'IEの起動
Dim objIE As Object
Set objIE = GetObject("", "InternetExplorer.Application")
objIE.Visible = True
objIE.Navigate "https://www.google.com/?hl=ja" ' このURLを任意に変更
' ページの表示完了待ち。
While objIE.ReadyState <> 4 Or objIE.Busy = True
DoEvents
Wend
On Error Resume Next '値がないとエラーが出るので、エラー回避用
objIE.document.getElementsByTagName("INPUT")(3).Value = "nujonoa_blog" 'テキスト欄に入力
Dim A As Object
For Each A In objIE.document.getElementsByTagName("INPUT") '①FOR EACH でINPUTをすべて抜き出す。
If A.Name = "btnK" Then A.Click '②IF分で入力したい要素を見つけて ③Click
Next
On Error GoTo 0
End Sub
ほかのやり方でテキストボックスに入力してみる。
また、単純に<INPUT>の4番目でもありますので、単純に、
objIE.document.getElementsByTagName("INPUT")(4).Click
と入力することで、クリックすることもできます。
Sub IE_buttonCLICK2()
'IEの起動
Dim objIE As Object
Set objIE = GetObject("", "InternetExplorer.Application")
objIE.Visible = True
objIE.Navigate "https://www.google.com/?hl=ja" ' このURLを任意に変更
' ページの表示完了待ち。
While objIE.ReadyState <> 4 Or objIE.Busy = True
DoEvents
Wend
objIE.document.getElementsByTagName("INPUT")(3).value="nujonoa_blog"
objIE.document.getElementsByTagName("INPUT")(4).Click
End Sub
その他、VBAでIEを動かすために必要な情報をまとめています!!
コメント欄
We're a group of volunteers and starting a new scheme in our community.
Your site offered us with valuable info to work on. You have done an impressive job and our whole community will be
grateful to you.
This design is incredible! You obviously know how
to keep a reader entertained. Between your wit and your videos, I
was almost moved to start my own blog (well, almost...HaHa!)
Excellent job. I really enjoyed what you had
to say, and more than that, how you presented it.
Too cool!
Hello! Do you know if they make any plugins to protect against hackers?
I'm kinda paranoid about losing everything I've worked
hard on. Any tips?
I seriously love your site.. Excellent colors & theme.
Did you make this web site yourself? Please reply back as I'm attempting to create my own personal blog and want to learn where you got this from or
exactly what the theme is called. Thanks!
Thank you for your comment!
The theme of this blog is "Cocoon", but it may be a theme limited to Japan.