make a program intract with something shown in web page - explorer

I want to build a program that interact with a web page, For example :
I have 3 lines A, B and C shown in the browser, when line A turned to green
I want to some value in variable. and so on with the other lines.
I don't want to open the page in my code, the page is already opened in windows explorer, and i want my code to interact with it where it is.
I hope that I explained my question clearly.

I try to access already opened web page using VBA code below but it is not able to get the text.
Sub demo()
Dim IEWindows As SHDocVw.ShellWindows
Dim IEwindow As SHDocVw.InternetExplorer
Dim IEDocument As MSHTML.HTMLDocument
Dim BreadcrumbDiv As MSHTML.HTMLElementCollection
Set IEWindows = New SHDocVw.ShellWindows
For Each IEwindow In IEWindows
'Debug.Print (IEwindow.LocationURL)
If InStr(IEwindow.LocationURL, "file:///C:/Users/Administrator/Desktop/demo17.html") <> 0 Then ' Found it
Set IEDocument = IEwindow.Document
Set BreadcrumbDiv = IEDocument.getElementById("demo1")
Debug.Print (IEwindow.Document.getElementById("data2").Value)
End If
Next
End Sub
As a work around, You can try to refer example below.
Sub demo()
Dim myIE As Object
Dim myIEDoc As Object
'Start Internet Explorer
Set myIE = CreateObject("InternetExplorer.Application")
'if you want to see the window set this to True
myIE.Visible = True
'Now we open the page we'd like to use as a source for information
myIE.navigate "C:\Users\Administrator\Desktop\demo17.html"
'We wait for the Explorer to actually open the page and finish loading
While myIE.Busy
DoEvents
Wend
'Now lets read the HTML content of the page
Set myIEDoc = myIE.Document
'Then we'll get something from teh inner page content by using the ID
If myIEDoc.all.Item("data1").Checked = True Then
Debug.Print (myIEDoc.getElementById("data1").Value)
ElseIf myIEDoc.all.Item("data2").Checked = True Then
Debug.Print (myIEDoc.getElementById("data2").Value)
Else
Debug.Print (myIEDoc.getElementById("data3").Value)
End If
End Sub
<!doctype html>
<head>
</head>
<body>
<input type="checkbox" id="data1" name="data" value="This is line 1."> This is line 1.<br>
<input type="checkbox" id="data2" name="data" value="This is line 2." checked> This is line 2.<br>
<input type="checkbox" id="data3" name="data" value="This is line 3."> This is line 3.<br>
</body>
</html>
Output in immediate window.
Further, you can try to modify code as per your requirement.

Related

Trying to get VBA to pull in data from Zillow

I am new to coding and have been trying to figure out how to extract specific data from zillow and import it into excel. To be honest I am pretty lost trying to figure this out and I have been looking throughout the form and other online videos, but I haven't had any luck.
Here is the link to the website I am using https://www.zillow.com/new-york-ny/home-values/
I am looking to pull all the numbers into excel so I can run some calculations. If someone could help me just pull in the Zillow Home Value Index of $660,000 into excel, I feel that I can figure out the rest.
This is the code from the website
<ul class="value-info-list" id="yui_3_18_1_1_1529698944920_2626">
<li id="yui_3_18_1_1_1529698944920_2625">
<!-- TODO: need zillow logo icon here -->
<!-- <span class="zss-logo-color"><span class="zss-font-icon"></span></span> -->
<span class="value" id="yui_3_18_1_1_1529698944920_2624">
$660,000
</span>
<span class="info zsg-fineprint"> ZHVI
</span>
I tried getElementsByTagName getElementById and getElemenByClass The id is confusing me since I want to be able to enter any town into excel and it will search on zillow for the data on the web page. All the id tags are different so if I search by id in this code it will not work for other towns. I used the Class tag and was able to get some of the data I was looking for.
This is the code I came up with It pulls into the text box the $660,000. The Range function is working and putting the text box data into excel. This is pulling a bunch of strings which I was able to pull out the $660,000, but the way the sting is set up Im not sure how to pull the remaining data, such as the 1 year forecast "yr_forcast" is the cell range I want to pull the data into excel.
Sub SearchBot1()
'dimension (declare or set aside memory for) our variables
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim y As Integer 'integer variable we'll use as a counter
Dim result As String 'string variable that will hold our result link
Dim Doc As HTMLDocument 'holds document object for internet explorer
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://www.zillow.com/new-york-ny/home-values/"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'in the search box put cell "A2" value, the word "in" and cell "C1" value
objIE.document.getElementById("local-search").Value = _
Sheets("Sheet2").Range("B3").Value & ", " & Sheets("Sheet2").Range("B4").Value
'click the 'go' button
Set the_input_elements = objIE.document.getElementsByTagName("button")
For Each input_element In the_input_elements
If input_element.getAttribute("name") = "SubmitButton" Then
input_element.Click
Exit For
End If
Next input_element
'wait again for the browser
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'price for home
Set Doc = objIE.document
Dim cclass As String
cclass = Trim(Doc.getElementsByClassName("value-info-list")(0).innerText)
MsgBox cclass
Dim aclass As Variant
aclass = Split(cclass, " ")
Range("Market_Price").Value = aclass(0)
Range("yr_forecast").Value = aclass(5)
'close the browser
objIE.Quit
End Sub
If you need anymore information please let me know.
The value you want is the first element with className value. You can use querySelector to apply a CSS selector of .value, where "." is the selector for class, to get this value.
Option Explicit
Public Sub GetInfo()
Dim html As New MSHTML.HTMLDocument
Const URL As String = "https://www.zillow.com/new-york-ny/home-values/"
html.body.innerHTML = GetHTML(URL)
Debug.Print html.querySelector(".value").innerText
End Sub
Public Function GetHTML(ByVal URL As String) As String
Dim sResponse As String
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", URL, False
.send
sResponse = StrConv(.responseBody, vbUnicode)
End With
GetHTML = Mid$(sResponse, InStr(1, sResponse, "<!DOCTYPE "))
End Function
You could also use:
Debug.Print html.getElementsByClassName("value")(0).innerText
Current webpage value:
Code output:

Web automation by VBA error - cannot click the button (javascript)

all.
I would like to click the button named id='#ICSearch'.Runtime error 424, object required. Since it is an on-click button that loads js. Will that be a problem?
I tried different codes but cannot get it. I am new to this please bear with me.
Private Sub IE_Autiomation()
Dim i As Long
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
'1.
IE.document.getElementById("ICSearch").Click
'2.
IE.document.getElementsByClassName("PSPUSHBUTTONTBADD")(0).Click
End Sub
<DIV class='' id='win0divSEARCHBELOW'><br/><br /><a class='PSPUSHBUTTON'
id = 'Left' role='presentation'><span style='background-Color: transparent;border:0;'>
<input type='button'
id='#ICSearch' name='#ICSearch' class='PSPUSHBUTTONTBADD'
value='Add' onclick="javascript:submitAction_win0(document.win0,
'#ICSearch');" tabindex='18' alt='Add (Alt+1)' title='Add (Alt+1)'/>
</span></a>
</DIV>
This is probably the smallest example of clicking of a button in InternetExplorer with VBA:
Option Explicit
Public Sub TestMe()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "https://github.com"
While IE.Busy
DoEvents
Wend
Dim objLoginLink As Object
Set objLoginLink = IE.Document.getElementsByClassName("text-bold text-white no-underline")
objLoginLink(0).Click
End Sub
This is how objLoginLink looks in the observer window:
The trick is that IE.Document.getElementsBySomething returns a collection, thus you can either loop through it or try to click on the first one.
Use Excel VBA to click on a button in Internet Explorer, when the button has no "name" associated

how to click a button on webpage having class name using vba excel

I am a rookie in VBA excel.
There is a web page application in which
i need to click a button, the source of which is
<em class="x-btn-arow" unselectable="on">
<button class= x-btn-text" id="ext-gen7576" style="" type="button">Actions</button>
Sub xx()
Dim IE As Object
Dim doc As HTMLDocument
Dim l As IHTMLElement
Dim lo As IHTMLElementCollection
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://theapplicationlink"
Do
DoEvents
Loop Until IE.ReadyState = 4
Set doc = IE.Document
Set lo = doc.getElementsByTagName("button")
For Each l In lo
If l.getAttribute("class") = "x-btn-text" Then
l.click
End If
Next
End Sub
it doesn't throw any error but it doesn't click the button.
I cannot use ID as it keeps on changing each time i launch the application.
Also the class and type is same for other buttons also.
Forgive me for any technical errors
Any help will be a huge favour here.
There is an id. Does it change completely or does part of it remain the same? If it were you could partial match on the bit that remain the same using a CSS selector.
That aside you could use:
objIE.document.querySelector("button[class*= x-btn-text]").Click
This uses a CSS selector to target the element of button[class*= x-btn-text]. Which will be the first element with button tag having attribute class with value containing x-btn-text.
"button" is not a HTML tag. use a "Tag". let me give you an example here. Replace the "strTagName" with a HTML tage that inlcudes the thing you want to click.
Dim objTag As Object
For Each objTag In objIE.document.getElementsByTagName(strTagName)
If InStr(objTag.outerHTML, "x-btn-text") > 0 Then
objTag.Click
Exit For
End If
Next

Filling Internet Explorer inputbox

I read so many answers to my problem but somehow if I try to "mimic" what I see, I still am not able to do what I need.
The problem is very simple: fill an inputbox on an opened IE page.
Result: the code gets stuck on the line with getelementbyid showing runtime error 424 (object required).
Private Sub AddInfoFromIntranet()
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
Application.SendKeys "{ESC}" ' I need this to ignore a prompt
With ie
.Visible = True
.navigate "{here goes the address of my website}"
Do Until Not .Busy And .readyState = 4
DoEvents
Loop
.document.getelementbyid("Nachnamevalue").Value = "{here goes whar I want to insert}"
End With
Set ie = Nothing
End Sub
Internet Explorer libraries were naturally imported (otherwise the "internetexplorer.application" wouldn't work.
I am positive that the field I want to fill is called "Nachnamevalue" as from what I learned this morning taking a look around the internet.
The html code of my webpage (only the interesting piece) looks like this:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
'{here there are info on the style, which i'm gonna ignore}
</style>
</head>
<body bgcolor="#ffffcc"><table width="1000"><tbody><tr><td>
<form name="Suchform" action="index.cfm" method="get" target="bottom_window">
Nachname:
<select name="Nachnamepulldown" class="font09px" onchange="wait_and_search()">
<option value="BEGINS_WITH">beginnt mit
<option value="EQUAL">ist
<option value="CONTAINS">enthält
</option></select>
<input name="Nachnamevalue" onkeyup="wait_and_search()" type="text" size="8">
Abteilung:
<select name="Abteilungpulldown" class="font09px" onchange="wait_and_search()">
<option value="BEGINS_WITH">beginnt mit
<option value="EQUAL">ist
<option value="CONTAINS">enthält
</option></select>
<input name="Abteilungvalue" onkeyup="wait_and_search()" type="text" size="3">
<input name="fuseaction" type="hidden" value="StdSearchResult">
<input type="submit" value="suchen">
<script language="JavaScript" type="text/JavaScript">
document.Suchform.Nachnamevalue.focus();
</script>
</form>
</td></tr></tbody></table></body>
</html>
There is also (I don't know if it can help) an "embedded" javascript that brings results of a search up every time at least 2 characters in the "Nachnamevalue" inputbox are written.
What am I doing wrong?
EDIT:
When I try to execute the Sub step-by-step, I get the following:
Set Doc = ie.document
? Doc
[object HTMLDocument]
( in the watchlist it is an object without any variables inside )
GetElementById gets an element by its id attribute, but "Nachnamevalue" is the value of the name attribute.
To use the name:
.document.Forms("Suchform").Elements("Nachnamevalue").value = "xxx"
This worked for me. The code uses HTML from your question in file c:\Temp\page1.html.
Option Explicit
' Add reference to Microsoft Internet Controls
' Add reference to Microsoft HTML Object Library
Sub AddInfoFromIntranet()
Dim ie As SHDocVw.InternetExplorer
Dim doc As MSHTML.HTMLDocument
Dim elements As MSHTML.IHTMLElementCollection
Dim nachnameValueInput As MSHTML.HTMLInputElement
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.navigate "c:\Temp\page1.html"
Do Until Not .Busy And .readyState = 4
DoEvents
Loop
Set doc = .document
Set elements = doc.getElementsByName("Nachnamevalue")
If Not elements Is Nothing Then
Set nachnameValueInput = elements(0)
If Not nachnameValueInput Is Nothing Then _
nachnameValueInput.Value = "{here goes whar I want to insert}"
End If
.Quit
End With
Set ie = Nothing
End Sub
To check the names of all input elements which exist at the momonet you execute the VBA code on the page you could use getElementsByTagName("input").
Set elements = doc.getElementsByTagName("input")
If Not elements Is Nothing Then
Dim inputElement
For Each inputElement In elements
Debug.Print inputElement.Name
Next inputElement
End If
You can try cycling all input and selecting the one is named as you need:
Set Elements = IE.document.getelementsbytagname("Input")
For Each Element In Elements
If Element.Name = "Nachnamevalue" Then
Element.Value = {Here your value}
Exit For
End If
Next Element

Visual Basic Word Get Elements from a Website

I'm trying to import text from specific div´s of a website to a bookmark in a Word document and I'm stuck with reading the HTML from a website. I tried 100 tutorials all for VBA Excel (maybe that's why) and always the same result.
Let's say that I have a site like:
<html>
<div id = "test">
this is an example text
</div>
</html>
and here is my VBA Code:
Sub read_html()
Set objIE = CreateObject("InternetExplorer.Application")
Dim htmlOut As String
With objIE
.Navigate "http://blabla.net/testy/test.html"
Do
Loop Until Not .Busy
htmlOut = .Document.getElementsByName("test")
.Quit
MsgBox "example:" & htmlOut
End With
Set iexpl = Nothing
End Sub
The MsgBox returns: example [object]
getElementsByName will return a collection of all the elements with that name. Even if there's only one, it still returns a collection. Just a collection with one item in it. You can get the first element in the collection like
htmlOut = .Document.getElementsByName("test").Item(0).InnerText
The .Item(0) will return the first item and the .InnerText will return a string.