Problem inserting value into "Input" field - vba

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

Related

Triggering <A> Element Without ID or Text

While setting up an automation process for extracting data from a page, I came across a link where I am having trouble selecting it.
Here is the HTML code:
<a class="dt-button buttons-copy buttons-html5" tabindex="0" aria-controls="tblReport" href="#" title="Copy to Clipboard"><span><i class="fa fa-files-o fa-lg blue"></i></span></a>
My last attempt was using:
For Each m In objIE.document.getElementsByTagName("a")
If m.className = "dt-button buttons-copy buttons-html5" Then
m.Click
Exit For
End If
Next
With Dim m As HTMLElementCollection.
But nothing happens.
If triggered, a window will appear stating that the content has been copied to the clipboard.
Another approach was:
objIE.document.getElementsByClassName("dt-button buttons-copy buttons-html5")(0).Click
But I get the error message
Object variable or With block variable not set
objIE is set as:
Dim objIE As Object
Set objIE = CreateObject("InternetExplorer.Application")
I can't figure out what I am missing.
You need to ensure that your page is completely loaded PRIOR to setting your object.
If you are already doing this, sometimes webpages will say they're loaded when they are actually not. To circumvent this, you can loop your object waiting for it to become ready by doing this:
Do While objIE.document.getElementsByClassName("dt-button buttons-copy buttons-html5")(0) Is Nothing
DoEvents
Loop
Then when it's no longer Nothing, you can now click it:
objIE.document.getElementsByClassName("dt-button buttons-copy buttons-html5")(0).Click

click link using getelementsbyclassname vba 7 using access 2007

I have a connection to an internet explorer webpage but i cannot seem to "click" a specific link using vba7 in access. I've tried loads of options like getelementsbyclassname. Here's the source code from the webpage:
<tr>
<td valign=bottom class="formbutton" align=center colspan=2>
view
</td>
</tr>
As you can see it has no name nor a usable hyperlink and the purpose of this link is to submit the filled-out form and give it a unique number on a new web page.
Can you please help me in VBA 7 for Access 2007 to come up with a vba code that works?
Many thanks!
The following should work assuming you have a pointer to the InternetExplorer object already.
Public Sub ClickElement()
'Assuming you already have an IE object
Dim Elements As Object
Dim Element As Object
'get all 'A' tags, which the hyperlink is part of
Set Elements = IE.Document.getElementsByTagName("a")
For Each Element In Elements
'I believe the element you want to click appears as 'View' on screen
If Element.InnerText = "View" Then
'Usually a good idea to give the element focus and
'possibly fire the OnClick event, but sometimes not needed
Element.Focus
Element.Click
Element.FireEvent ("OnClick")
Exit For
End If
Next
End Sub

VBA to change dropdown value in internet explorer

I am looking to automate internet explorer using Excel VBA to extract football results from a website and am really struggling with getting the data to update when I change the dropdown value.
The website is: http://www.whoscored.com/Regions/250/Tournaments/30/Seasons/3871/Stages/8209/Fixtures/Europe-UEFA-Europa-League-2013-2014
I am looking to change the value of the 'stages' dropdown and scrape the match results.
My code works fine for opening IE, changing the value of the 'scrape' dropdown but I can't get the data to update. Whilst I am comfortable with VBA I know very little about HTML and Javascript although I can guess what some lines are doing. From what I can see there is javascript code that handles the change event, I just can't see how to get it to run - I have tried firing the 'onchange' event in my code as suggested from my searches but I can't get it to work.
This is the code I can see that controls the drop down (I have deleted a lot of the dropdown values for other dropdowns as it made this post even longer:
<div id="breadcrumb-nav">
.
.
<span><select id="stages" name="stages"><option selected="selected" value="/Regions/250/Tournaments/30/Seasons/3871/Stages/8209">Europa League Group Stages</option>
<option value="/Regions/250/Tournaments/30/Seasons/3871/Stages/7816">Europa League Qualification</option>
<option value="/Regions/250/Tournaments/30/Seasons/3871/Stages/8158">Europa League Grp. A</option>
<option value="/Regions/250/Tournaments/30/Seasons/3871/Stages/8159">Europa League Grp. B</option>
.
.
<option value="/Regions/250/Tournaments/30/Seasons/3871/Stages/8466">Europa League</option>
</select></span>
</div>
<script type="text/javascript">
$('#breadcrumb-nav select').change(function () {
NG.GA.trackEvent('BreadcrumbNav', this.id);
window.location.href = this.value;
// TODO: Disable all selects?
});
</script>
my code:
Sub ScrapeData()
Dim ie As InternetExplorer
Dim URL As String
URL = "http://www.whoscored.com/Regions/250/Tournaments/30/Seasons/3871/Stages/8466/Fixtures/Europe-UEFA-Europa-League-2013-2014"
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate (URL)
Do
DoEvents
Loop Until ie.readyState = 4
SelectValue ie, "/Regions/250/Tournaments/30/Seasons/3871/Stages/7816"
SelectValue ie, "/Regions/250/Tournaments/30/Seasons/3871/Stages/8209"
End Sub
Sub SelectValue(ByVal ie As InternetExplorer, ByVal value As String)
Dim htmlDoc As HTMLDocument
Dim ddStages As HTMLSelectElement
Dim idBreadCrumb As Object
Set htmlDoc = ie.document
With ie.document
Set idBreadCrumb = .getelementbyid("breadcrumb-nav")
Set ddStages = .getelementbyid("stages")
End With
ddStages.value = value
ddStages.FireEvent ("onchange")
'fireevent on ddStages didn't work so tried here too
idBreadCrumb.FireEvent ("onchange")
Do
DoEvents
Loop Until ie.readyState = 4
End Sub
Any help would be really appreciated.
There must be some JavaScript executing on the event "the select element has changed its value". My suggestion, much easier than executing the JavaScript, is to just navigate the link (because what the JS does here is just changing the HTML page you are seeing, and not the elements within the same webpage).
So, for example, I would just replace this:
SelectValue ie, "/Regions/250/Tournaments/30/Seasons/3871/Stages/7816"
with this
ie.Navigate "http://www.whoscored.com/" & "/Regions/250/Tournaments/30/Seasons/3871/Stages/7816"
to get the exactly same result.

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

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