Using Excel VBA to input a value into Internet Explorer textbox - vba

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

Related

How to display the content of a TextArea from the HtmlDocument of a WebBrowser Control

I have a TextArea in a page of my Site, I want to display the text of this TextArea, using the Document of a WebBrowser Control, to a Label.
This is the TextArea definition:
<textarea class="cadr" id="bar" name="saisie"style="height: 260px; width: 700px;"> simple text</textarea>
How can I do it? I have this code:
WebBrowser1.Document.GetElementById(label1.text).GetAttribute("value", "bar")`
The Element you describe has no ID, so you have to resort to the className of both the TEXTAREA and its DIV parent (to narrow down the chance to get the wrong element).
<div class="labelapper"><textarea rows="3" cols="80" class="Label">text1</textarea></div>
You can use GetElementsByTagName() instead of GetElementsById(), then filter the results
Something like this:
Imports System.Linq
' [...]
dim innerText = String.Empty
dim textArea = WebBrowser1.Document.GetElementsByTagName("TEXTAREA").
OfType(Of HtmlElement)().
FirstOrDefault(Function(elm) elm.GetAttribute("className") = "Label" AndAlso
elm.Parent.GetAttribute("className") = "labelapper")
if textArea IsNot Nothing Then
innerText = textArea.InnerText
End If
EDIT: The HtmlElement in the original post has been changed, now showing an ID
dim textArea = WebBrowser1.Document.GetElementById("bar")
if textArea IsNot Nothing Then
label1.Text = textArea.InnerText
End If

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")

VBA to fetch attribute values from HTML

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

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