How to add a tick to a checkbox through excel vba ie automation? - excel

The checkbox I am trying to add a tick to is part of an online table and doesn't seem to be coded as a checkbox.
I have tried the following to add a tick and none work:
IE.Document.getElementByID("gridcolumn-1658-titleEl").Click
IE.Document.getElementByID("gridcolumn-1658-textEl").Click
IE.Document.getElementsByClassName("x-column-header-inner x-column-header-over")(0).Click
IE.Document.getElementsByClassName("x-column-header-inner")(0).Click
When I inspect the element, I get to the following, but none of this looks anything like a checkbox (the third one is the actual reference):
<div class="x-column-header x-column-header-checkbox x-column-header-align-left x-box-item x-column-header-default x-unselectable x-column-header-first" style="border-width: 1px; width: 24px; right: auto; left: 0px; top: 0px; margin: 0px; height: 24px;" id="gridcolumn-1658"><div id="gridcolumn-1658-titleEl" class="x-column-header-inner" style="padding-top: 6px; padding-bottom: 6px;"><span id="gridcolumn-1658-textEl" class="x-column-header-text"> </span></div></div>
<div id="gridcolumn-1658-titleEl" class="x-column-header-inner" style="padding-top: 6px; padding-bottom: 6px;"><span id="gridcolumn-1658-textEl" class="x-column-header-text"> </span></div>
<span id="gridcolumn-1658-textEl" class="x-column-header-text"> </span>
The website is OptimoRoute, which can be accessed fairly quickly using a new log in, for those interested!
The button I am trying to click is the top one in the table.

Please refer to the following sample code:
Sub main()
'we define the essential variables
Dim IE As Object, Data As Object
Dim ticket As String
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate ("https://dillion132.github.io/vbacheckbox.html")
While IE.ReadyState <> 4
DoEvents
Wend
Set Data = IE.Document.getElementsByClassName("check")
Debug.Print Data.Length
If Len(Data) > 0 Then
For Each ee In Data
'Debug.Print ee.Value
'Based on the checkbox value to check/uncheck the checkbox.
If ee.Value = "Cat" Then
ee.Checked = True
End If
'check whether the checkbox is checked, then, get the checked value.
If ee.Checked Then
Debug.Print ee.Value & " is checked"
End If
Next ee
End If
End With
Set IE = Nothing
End Sub
Code in the website:
<input class="check" type="checkbox" value="Cat"> Cat </input>
<br />
<input class="check" type="checkbox" value="Dog" >Dog</input>
<br />
<input class="check" type="checkbox" value="Pig" checked ="checked" >Pig</input>
The result as below:

Related

Unable to extract Text using VBA, when there are multiple Classname , tagname and attributes with same name

I am trying to extract the innertext under the 'span' class from a IE browser, but facing an issue. Below is the HTML code
1st html snippet
<div class="SectionStart" style="display: block;" oraload="oraDisplayNone(item,'newDev','Y','=')" oraInitDisplayStyle="block">
<label oramdlabel="CM_SN">Serial Number</label>
<!--style tag has been added-->
<span class="oraDefault" style="left: -5px; top: 4.5px; position: relative;" orafield="MSerialNumber" oraType="string;precision:20" oraErrorElement="mSerialNumber">G4A001</span>
</div>
2nd html snippet
<div class="SectionStart">
<label oramdlabel="CM_SN">Serial Number:</label>
<!--style tag has been added-->
<span class="oraDefault" style="left: -5px; top: 4.5px; position: relative;" orafield="MSerialNumber" oraType="string;precision:20" oraErrorElement="mSerialNumber">E5W807</span>
</div>
Here the issue is as per below code ,while trying to fetch the 2nd value "E5W807", I am only fetching the not required value "G4A001".
Please guide how can I tackle the problem of ONLY extracting the text "E5W807".
Set objSubCollec = objCollection(0).contentWindow.document.getElementById("Page")
Set objElement = objSubCollec.contentWindow.document.getElementById("Frame_4")
objElement.Focus
Set objElement1 = objElement.contentWindow.document
Set elm2 = objElement1.getElementsByTagName("span")
For Each e1 In elm2
If e1.getAttribute("orafield") = "MSerialNumber" Then
temp = e1.innerText
'MsgBox temp
Worksheets("Sheet1").Range("B6") = temp
Exit For
End If
Next
Maybe Try this:
It will print all the found numbers in succession from B6.
Set objSubCollec = objCollection(0).contentWindow.document.getElementById("Page")
Set objElement = objSubCollec.contentWindow.document.getElementById("Frame_4")
objElement.Focus
Set objElement1 = objElement.contentWindow.document
Set elm2 = objElement1.getElementsByTagName("span")
i = 6
For Each e1 In elm2
If e1.getAttribute("orafield") = "MSerialNumber" Then
temp = e1.innerText
Worksheets("Sheet1").Range("B & i") = temp
i = i + 1
End If
Next

Scraping IE with VBA, how to scroll to bottom of page?

I am pretty new to VBA. I'm trying to scroll to the bottom of a web page. Below is contents of web page.
<DIV id="Table">
<div style="width: 18px; overflow: auto; height: 912px;" onscroll="Grids[0].Scrolled();" onmousemove="Grids[0].ARow=null;Grids[0].ASec=-1;"><div style="width: 1px; overflow: hidden; height: 1954px;"> </div></div>'
This is VBA Code I have so far.
Dim selector As String
selector = "#Table div[style*='onmousemove']"
Dim rightWindowDiv As HTMLDivElement
Set rightWindowDiv = htmldoc.querySelector(selector)
rightWindowDiv.ScrollTop = rightWindowDiv.ScrollHeight
Somehow it does not work. Can someone help?

Clicking an item on a drop-down menu with VBA

I am having trouble clicking an item on a drop-down menu with VBA. I am able to click the drop-down button to show the options, but I cant click on a specific item. Below is the item I want to click on.
<a class="ActiveLink" title="Excel" alt="Excel"
onclick="$find('ctl31').exportReport('EXCELOPENXML');"
href="javascript:void(0)" style="padding: 3px 8px 3px 32px; display: block;
white-space: nowrap; text-decoration: none;">Excel</a>
There are 5 possible selections in the dropdown. "title" and "alt" are unique to this selection. The "class" is unique to all items of the dropdown. I tried the following.
IE.Document.getElementsByClassName("ActiveLink")(4).Click
I am aware that the 4 indicates the 5th element.
The error is
Object doesn't support this property or method.
I have resolved with the following code.
Set HTMLdoc = IE.Document
For Each l In HTMLdoc.getElementsByTagName("a")
If l.Title = "Excel" Then
l.Click
Exit For
End If
Next
End Sub

Read straight web content with Excel VBA

there are many article on this site on how to read tags and tables in web sites with Excel VBA, but I am stuck here.
This website gives me business locations after entering a Zip code.
("Where is the closest location relative to my Zip Code")
I managed to navigate to the site, enter the Zip code and click Submit:
Dim Browser As SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Set Browser = New SHDocVw.InternetExplorer ' create a browser
Browser.Visible = True ' make it visible
Application.StatusBar = ".... opening page"
Browser.navigate "https://www.thewebsite.com" ' navigate to page
WaitForBrowser Browser, 1 ' wait for completion or timeout
Application.StatusBar = "gaining control over DOM object"
Set HTMLDoc = Browser.document ' load the DOM object
WaitForBrowser Browser, 1
HTMLDoc.getElementById("ZipCode").Value = "28278"
HTMLDoc.getElementById("localTeamZipSubmit").Click
The site opens and the relevant content looks like this:
<div>
<div class="columns">
<div class="column boldText paddingFive" style="padding-left: 20px; width: 70px;">
Location:
</div>
<div class="column paddingTopFive">CHARLOTTE</div>
</div>
<div class="columns">
<div class="column boldText paddingFive" style="padding-left: 20px; width: 120px;">
Location Number:
</div>
<div class="column paddingTopFive">102340</div>
</div>
<div class="columns">
<div class="column boldText paddingTopFive paddingLeftTwenty" style="vertical-align: top;">
Address:
</div>
<div class="column paddingTopFive paddingLeftTwenty">
<div>8848 Main St.</div>
<div>Suite F</div>
<div></div>
<div>Charlotte, NC 27218</div>
</div>
</div>
<div class="columns">
<div class="column boldText paddingFive" style="padding-left: 20px; width: 70px;">
Phone:
</div>
<div class="column paddingTopFive">(704) 911-4440</div>
</div>
<div class="columns">
<div class="column boldText paddingFive" style="padding-left: 20px; width: 70px;">
Fax:
</div>
<div class="column paddingTopFive">(704) 911-4441</div>
</div>
</div>
As you can see, this section has no table, no named tags and classes that are use over and over.
I was not able to read this information yet. I would be happy to get the whole blob into a String and parse it"
"Text = HTMLDoc.getEverything()"
Thanks a lot for your help!!!
In the meantime I found another code snippet that I modified but I am getting stuck at the same point:
Post and submit works but how to get the answer....
{ Private Sub PostalCodes()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
On Error GoTo errHandler
ie.Visible = 1
With ie
.navigate "https://www.pattersondental.com/ContactUs/MyLocalTeam"
Do While .busy: DoEvents: Loop
Do While .ReadyState <> 4: DoEvents: Loop
With .document.Forms("GetBranchFromZipForm")
.ZipCode.Value = "28273"
.submit
End With
' Do While Not CBool(InStrB(1, .document.URL, _
' "cp_search_response-e.asp"))
' DoEvents
' Loop
Do While .busy: DoEvents: Loop
Do While .ReadyState <> 4: DoEvents: Loop
' MsgBox .document.all.tags("Colums").Item(1).Rows(1).Cells(1).innerText
MsgBox .document.all.tags("Colums").innerText
' MsgBox .document}
I guess I have to search no for "how to dissect a HTML document"...
Add on:
It seems that while ie is a valid item (in the watch window) IE.Document is empty... why can this be, The website is still there with new data.
I even tried another code snippet that looks for open websites in IE, it finds the site (with the correct data) but the document is still empty and getelementBY... does not find anything of course.
I am about to start drinking...
I can't believe it.
After 3 days of poking I found this:
With ActiveSheet.QueryTables.Add(Connection:="URL;
https://www.pattersondental.com/ContactUs/MyLocalTeam",
Destination:=Range("A1"))
.PostText = "ZipCode=70032"
.RefreshStyle = xlOverwriteCells
.SaveData = True
.Refresh
I don't pretend to understand why it works, but is does.
John, I will still check out, what you suggested. Thanks

Item Selection from combo-box using vba in Internet Explorer

The below values represent a combobox / combo-list on a website. I am trying to select item #3 from the drop down using VBA. I have tried several things, but I have had no luck selecting said item. I have tried looking for solutions on Google and the Stack but haven't found anything that seems to work.
<DIV id=ext-gen256 class="x-layer x-combo-list x-combo-list-small" style="FONT-SIZE: 10px; HEIGHT: 92px; WIDTH: 113px; POSITION: absolute; LEFT: 744px; Z-INDEX: 12007; TOP: 235px; VISIBILITY: visible">
<DIV id=ext-gen257 class=x-combo-list-inner style="HEIGHT: 90px; WIDTH: 111px">
<DIV class="x-combo-list-item" _nodup="30829" viewIndex="0">Select</DIV>
<DIV class="x-combo-list-item" _nodup="30829" viewIndex="1">Item 1</DIV>
<DIV class="x-combo-list-item" _nodup="30829" viewIndex="2">Item 2</DIV>
<DIV class=""x-combo-list-item" _nodup="30829" viewIndex="3">Item 3</DIV>
<DIV class="x-combo-list-item" _nodup="30829" viewIndex="4">Item 4</DIV></DIV></DIV>
I have used several variatons of the below code to try and select the item in question (i.e item #3), but keep getting a runtime error
Dim inputE As MSHTML.HTMLHtmlElement
Set inputE = IE.document.getElementsByClassName("x-combo-list-item")
If inputE.innerText = "Item 3" Then
inputE.Select
End If
This doesn't seem to work. Any help is greatly appreciated. Thanks!

Resources