5

I have a webpage that I am extracting data from. I can do everything fine with VBA apart from clicking on an image element which then submits a form and a popup with data is created.

One of the attributes in the image element is called "productguid" and has a string value =

"a12-545".

The HTML code of the form looks like this BEFORE I click on the image element with my mouse.

<form id="GetProductQuantitiesForAccessibleBranches" action="GetProductQuantitiesForAccessibleBranches" method="post">
  <input type="hidden" name="ProductGuid" value="">
</form>

This is the HTML code AFTER I manually click on it with the mouse:

<form id="GetProductQuantitiesForAccessibleBranches" action="GetProductQuantitiesForAccessibleBranches" method="post">
  <input type="hidden" name="ProductGuid" value="a12-545">
</form>

Based on this I assumed that the productguid value from the image element gets passed onto the form before it is submitted. Here is what my VBA code looks like this:

'Change the input element value
ie.Document.getElementById("GetProductQuantitiesForAccessibleBranches").all.item(0).value = "a12-545"

'Submit the form
ie.Document.getElementyId("GetProductQuantitiesForAccessibleBranches").Submit

According to the Locals window, all the Javascript events are Null. Both lines of code run without any errors but the webpage does not update. Am I missing something here?

I have also tried just clicking the image element with the .Click method, but that doesn't do anything either.

The webpage is password protected so I cannot post the URL publicly.

UPDATE:

Here is the HTML in the tag that normally is clicked manually which then submits the form. Perhaps there is something in here that I can use?

<img alt="View Quantities At Other Locations" src="/WebOrder/Images/CheckQtys.gif" 
title="View Quantities At Other Locations" class="popup" 
popupdirection="upperleft" popupwidth="380" 
popupcontent="#ProductQuantitiesForAccessibleBranches" 
onbeforepopupcreate="onBeforePopupCreate_GetProductQuantitiesForAccessibleBranches(this)" 
popupajaxformid="GetProductQuantitiesForAccessibleBranches" 
onbeforepopupajaxpost="onBeforePopupAjaxPost_GetProductQuantitiesForAccessibleBranches(this)" 
oncompletepopupajaxpost="onCompletePopupAjaxPost_GetProductQuantitiesForAccessibleBranches(this)" 
productguid="a12-545" 
displayitem="33902" 
brandguid="00000000-0000-0000-0000-000000000000" 
brandname="" brandsku="">
  • If the value of a page element is changing based on a click event, I'd bet that all the javascript events aren't null. The IE object might not be showing some events for some reason, but digging into that javascript is definitely going to be key to understanding what's going on. Investigate the scripts/events on the page yourself with e.g. the dev console of your favorite browser. I also highly suggest adding the javascript and/or dom tags to get some experts in here. – Mikegrann Aug 23 '16 at 22:15
  • Oh ok, thanks. I tired using Chrome Dev but I'm having a hard time understanding what I'm looking at... If I click on the Networks tab, in the Initiator column I see a script, then when I mouseover it, a whole list of different things show up. What am I supposed to do with this information? – TheGuyOverThere Aug 23 '16 at 22:29
  • To set the value something like: `ie.Document.getElementById("GetProductQuantitiesForAccessibleBranches").getElementsbyName("ProductGuid")(0).Value=Whatever` should work – Ryan Wildry Aug 24 '16 at 18:12
  • Changing the value is fine, it does that. Submitting is the issue. The post request simply does not go through. – TheGuyOverThere Aug 24 '16 at 23:35
  • If you are trying to submit the form, try: `ie.Document.Forms("GetProductQuantitiesForAccessibleBranches").Submit` – Ryan Wildry Aug 26 '16 at 12:44
  • Unfortunately that doesn't work either. The line runs fine, no errors, but the submit action does not go through. – TheGuyOverThere Aug 26 '16 at 14:26
  • Two other thoughts, after submitting the form as shown above try firing the onSubmit event. Something like `ie.document.Forms("GetProductQuantitiesForAccessibleBranches").fireEvent("onSubmit")`. The other thought is creating a POST request, it looks like that might be what the form is doing when it is submitted. See this for a start http://stackoverflow.com/questions/158633/how-can-i-send-an-http-post-request-to-a-server-from-excel-using-vba – Ryan Wildry Aug 26 '16 at 15:52
  • If you have a submit button on the form you can try this alternative: `ie.Document.getElementById("SubmitButtonID").Click` – Thomas G Aug 27 '16 at 10:06
  • I already tried the `.Click` method. – TheGuyOverThere Aug 29 '16 at 19:14
  • @TheGuyOverThere find the node where the image resides. find the image within the node by tagName("img") perform a click on that image/Object -> wait for ieObject to reload -> this should work?? – Krish Aug 31 '16 at 16:32
  • Sadly it does not. That was my first approach – TheGuyOverThere Aug 31 '16 at 22:51
  • 1
    Could it be as simple as using submit() instead of Submit? Not 100% sure on how VBA sends javascript commands.. – Steve Sep 01 '16 at 01:38

1 Answers1

3

Give this a shot, I doubt this is going to be the 'perfect' answer without actually seeing the site. However, this should find the correct image tag, assuming you don't have any frames/iframes (please check there aren't any), and should click it.

Public Sub Click_IMG_Tag()
    Dim Elements As Object
    Dim Element  As Object

    'Get a pointer to IE etc first, I'm assuming this is already done

    Set Elements = IE.Document.getElementsByTagName("img")

    For Each Element In Elements
        On Error Resume Next ' used to bypass elements that don't have .Title property
                             ' later, you should error handle this exception
        If Element.Title = "View Quantities At Other Locations" Then
            Element.Focus
            Element.Click
            Element.FireEvent ("OnClick")
            IELoad IE
            Exit For
        End If
    Next

End Sub

Public Sub IELoad(Browser As Object)
    While Browser.busy Or Browser.Readystate <> 4
        Application.Wait (Now() + TimeValue("00:00:01"))
        DoEvents
    Wend
End Sub
Ryan Wildry
  • 5,612
  • 1
  • 15
  • 35
  • I was about to answer something similar. I once had the same problem, and then had to loop through all the buttons and check for the one with a certain label, and then click it. This seems like a great answer to me. IE-automation with vba is a pain in the ass anyways. good job! – Mafii Sep 01 '16 at 08:39
  • Thanks for the feedback! – Ryan Wildry Sep 01 '16 at 20:58
  • Thanks for trying, I know you've spent quite a lot of effort trying to help me figure this out, but this also does not work. Code runs fine, but webpage does not update. I am literally completely out of ideas. – TheGuyOverThere Sep 01 '16 at 22:36
  • There aren't any frames? – Ryan Wildry Sep 02 '16 at 01:01
  • None that I can see. I am able to interact and extract information from the page without any issues. There are over 50 `` tags on each page and each is part of a row that I already extract data from – TheGuyOverThere Sep 06 '16 at 13:23