Felix_Dragonhammer Felix_Dragonhammer - 2 years ago 251
HTML Question

VBA - getElementById works for simple website but not another?

So I'm currently stuck at getting a VBA script to retrieve the value of an input box from this Sudoku website. However, I was able to get the value from a paragraph element with the id of "contact" from my own simpler website, shaneberger.com using the same code (after switching the url and id names, of course).

Any attempts to research further brings up articles/blogs that discuss what I've done correctly so far, so I suspect I am not researching it properly.

Here is my code:

Sub GetTable()

Dim ieApp As InternetExplorer
Dim ieDoc As Object
Dim sudokuCell As Object
Dim url, id, content As String
Dim i As Integer

Set ieApp = New InternetExplorer

ieApp.Visible = True

url = "http://www.websudoku.com/"
ieApp.navigate url

Do While ieApp.Busy: DoEvents: Loop

Set ieDoc = ieApp.document

If ieDoc Is Nothing Then
MsgBox ("Nothing")
' MsgBox ("Something")
End If

For i = 0 To 8
Set sudokuCell = ieDoc.getElementById("f00")
content = sudokuCell.innerText
MsgBox (content)
Next i

Set ieApp = Nothing

End Sub

And here is an example of the html for a cell which is blank:

<td class="g0" id="c00"><input class="d0" size="2" autocomplete="off"
name="8iz6n11" maxlength="1" onblur="j8(this)" id="f00"></td>

And here is one for cell that is prefilled with a number:

<td class="f0" id="c10"><input class="s0" size="2" autocomplete="off"
name="s8iz6n21" readonly="" value="7" id="f10"></td>

I have tried both the "c00" an "f00" without success. Also, while I believe the problem at hand is I am not retrieving the element, I am concerned that the .innerText property won't retrieve the values.

Answer Source

First: The website is using FRAME, so you are not accessing the frame document in the VBA code actually. You need to navigate to the actual URL given below - change your url variable as the following (which is the frame's src property):

url = "http://view.websudoku.com/?"

Second: Those are INPUT elements you are trying to get values, you should be better using Value property instead innerText

content = sudokuCell.Value

Third and last: I have no idea what your code is supposed to do inside the loop as it will keep reading f00 element value as is. However I believe you'll loop through the input elements and just hit the wall here about the FRAME issue I explained above, so I assume loop is your part and have no trouble about it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download