VBA to fetch attribute values from HTML - vba

I am trying to create macro where it will fetch the data from particular attribute, and simply paste on excel, I tried it using getElementsById, but its not working for me.
I have link:
https://secure.bmw.com/_common/silo/pages/form-tabs/controller_opt.jsp?locale=XQ_en&usercountry=IN&tracking_usercountry=IN&tracking_locale=XQ_en&transfer=f23_info_req_lp
Just view source it and you will find the attribute name "lead context"
<input type="hidden" name="reg_source" value="OPK">
<input type="hidden" name="lead_context" value="F45CV ROI OPK">
<input type="hidden" name="subscription_lead_context" value="F45CV ROI OPK">
<input type="hidden" name="cam_source_code" value="1-1521452141">
<input type="hidden" name="offer_code" value="1524521452">
So basically I want value "OPK", "F45CV ROI OPK", "1-1521452141" and "1524521452" on excel sheet.
Can anyone help me with simple code, Sorry for as i am new in VBA.
Thanks for your time :)
Regards,
Mayur

One method is grab all <input> elements and cycle through them to collect what you need. The variable assignment I've used requires Tools, References, Microsoft HTML Object Library but you could just call them Objects if you preferred.
Dim eNPT As MSHTML.IHTMLElement, ecNPTs As MSHTML.IHTMLElementCollection
Set ecNPTs = ie.document.getElementsByTagName("input")
Range("A1:A5").ClearContents
For Each eNPT In ecNPTs
Select Case LCase(eNPT.getAttribute("name"))
Case "reg_source"
Range("A1") = eNPT.Value
Case "lead_context"
Range("A2") = eNPT.Value
Case "subscription_lead_context"
Range("A3") = eNPT.Value
Case "cam_source_code"
Range("A4") = eNPT.Value
Case "offer_code"
Range("A5") = eNPT.Value
End Select
If Application.CountA(Range("A1:A5")) = 5 Then Exit For
Next eNPT
Set ecNPTs = Nothing: Set eNPT = Nothing

Related

How can I get selected data from a combobox on a website into Excel with Selenium VBA?

I want data from the website. The number comes up, the name does not.
I want the name.
Sayfa1.Cells(row, 19).Value = bot.FindElementByName("BeyanAdresi.IlKodu").Value
"select class="form-control valid" data-val="true" data-val-number="The field İl must be a number." data-val-required="İl Alanı Gereklidir" id="BeyanAdresi_IlKodu" name="BeyanAdresi.IlKodu"><option value="">Lütfen Seçiniz</option
option value="1">ADANA/option>option value="2">ADIYAMAN/option>option value="3">AFYONKARAHİSAR/option>option value="4">AĞRI/option>option value="68">AKSARAY/option>option value="5">option>..."
You can try to put .text instead of .value. Just like:
Sayfa1.Cells(row, 19)= bot.FindElementByName("BeyanAdresi.IlKodu").Text

Problem inserting value into "Input" field

I run into a weird problem when trying to automate inserting data into a website. Specifically one input field, here is html piece of that website:
<input class="maskAcctNbr" id="masked_consumer" type="text" size="20" maxlength="20" value="" autocomplete="off">
When I will insert value into that field using this code and click on "Lookup" button:
Dim SE As MSHTML.IHTMLElement
Set SE = HTMLDoc.getElementById("masked_consumer")
SE.Value = "574844"
The page is telling me that I'm missing information.
After I will manually click on that field and enter that number, the underlying html code changes to:
<input class="maskAcctNbr" id="masked_consumer" type="text" size="20" maxlength="20" value="" autocomplete="off" real="574844">
New real property shows up.
Any ideas how I can solve that using VBA? I tried, click and focus, and nothing really works, until I use mouse to click on that field and manually enter that number.
Thank you!
Try to refer example below may help you to solve the issue.
Example:
Sub demo()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate "http://example.com"
IE.Visible = True
While IE.Busy
DoEvents
Wend
Set TrackID = IE.document.getElementById("masked_consumer")
TrackID.Focus
Application.SendKeys ("(574844)"), True
Application.SendKeys ("{ENTER}"), True
End Sub
I ran into a similar issue one time, and activating .OnClick event of that field helped.
In your case it would be
SE.onclick
before setting the value.
There can also be some other events such as OnChange that may result in "no data entered" errors, when they are not triggered.
Good luck :)

GetElementById isn't working on certain boxes

I'm trying to make an automatic login in my program.
This is the code I'm using to find the email box and then set the value:
WebBrowser1.Document.GetElementById("email").SetAttribute("value,", ID & "#hotmail.com")
If I insect the element of the email box it says input class="textbox" type="email" name="email"
What am I doing wrong? :L
The element doesn't necessarily have to have an ID.
http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementsbytagname(v=vs.110).aspx
If that's the case, you should take a look at getting a collection via GetElementsByTagName and looping through - something along these lines:
Dim Elems As HtmlElementCollection
Elems = WebBrowser1.Document.GetElementsByTagName("input")
For Each elem As HtmlElement In Elems
Dim nameValue As String = elem.GetAttribute("name")
If nameValue.ToLower().Equals("email") Then
elem.SetAttribute("value,", ID & "#hotmail.com")
End If
Next
Refer to id instead of name. i.e. "email_ema" instead of "email"
input name="email" type="button" id="email_ema" value="+" style="display:None;"...
WebBrowser1.Document.GetElementById("email_ema").SetAttribute("value,", ID & "#hotmail.com")

Using Excel VBA to input a value into Internet Explorer textbox

New to VBA/StackOverflow/etc. My code currently opens Internet Explorer, navigates to a URL, checks to see if it's logged in, logs in if necessary, then navigates to a new page.
Now I'd like to input a string value into an Internet Explorer textbox.
Here's the relevant elements' HTML source code:
<input type="text" class="search_customDate" name="dateFrom" id="dateFrom"
value="mm/dd/yy" size="13"
onchange="RTSDHTMLUTIL.getRTSDHTML('dateFrom').onChange(event);"
OnFocus OnBlur />
<input type="text" class="search_customDate" name="dateTo" id="dateTo"
value="mm/dd/yy" size="13"
onchange="RTSDHTMLUTIL.getRTSDHTML('dateTo').onChange(event);"
OnFocus OnBlur /> <INPUT id="dateTo" class="search_customDate"
<input type="text" class="search_companyLookupField" name="tokens" id="tokens"
value title OnChange OnMouseOver OnMouseOut OnKeyDown="doLookup(event);"
OnKeyUp OnFocus OnBlur OnDisable />
Here's relevant portion of my code:
'Input From and To dates
Dim DateFrom As Object ' MSHTML.HTMLInputElement
Set DateFrom = AppIE.Document.getElementByID("dateFrom")
If Not DateFrom Is Nothing Then
DateFrom(0).Value = "05/05/05"
End If
Dim DateTo As Object ' MSHTML.HTMLInputElement
Set DateTo = AppIE.Document.getElementByID("dateTo")
If Not DateTo Is Nothing Then
DateTo(0).Value = "06/06/06"
End If
Dim Ticker As Object ' MSHTML.HTMLInputElement
Set Ticker = AppIE.Document.getElementByID("tokens")
If Not Ticker Is Nothing Then
Ticker(0).Value = "XYZ"
End If
As of now, nothing appears in the text boxes and I can't seem to figure out why. Would love anyone's input/advice.
Thanks
That's where I would start:
remove the "(0)" after your HTML element variables names - the getElementByID method returns unique elements, not collections
make sure that the HTML input elements you are trying to retrieve aren't actually returning nothing

Block a certain element in web browser Vb.net

I a wondering if and how you would be able to block a certain element in the webbrowser.
For example there is a certain element on a website:
<div class="btn" id="button" value="ClickMe"/>
Will you be able to block or hide that specific element in the webbrowser?
There might be a certain ad, iframe or whatever its called that might just be in the way?
Thanks for the help I appreciate it.
double click on webbrowser1 designer you should be in WebBrowser1_DocumentCompleted add this line
'selector by division id
webbrowser1.Document.getElementById("button").innerHtml = ""
'selector spesific element with spesific attribute
<input id="_authentication_token" name="_authentication_token" type="hidden" value="11740686"/>
dim mycol as htmlElementCollection = webbrowser1.document.getElementBytagname("input")
for each ele as Htmlelement in mycol
if (ele.getAttribute("value").tostring = "11740686") then ele.innerHtml = ""
next
for your exemple :
<div class="btn" id="button" value="ClickMe"/>
dim mycol as htmlElementCollection = webbrowser1.document.getElementBytagname("div")
for each ele as Htmlelement in mycol
if (ele.getAttribute("value").tostring = "ClickMe" and ele.getAttribute("id").tostring = "button" ) then ele.innerHtml = ""
next