3

I have been trying to automate a process I do on a website with VBA but my knowledges of JavaScript are limited. I have asked things regarding this but haven't got a solution yet.

What I need to do is to answer a series of Y/N questions which work like this, for example the question has two more sub-questions related. If you choose 'Yes' in the first one the second one appears and so on. The thing is that I can't make the VBA to trigger all the events that website has meaning I can answer 'Yes' to the second question (the first is always already answered manually when I start working) now it won't show the third and last question.

First two questions Three questions

I have looked at the JS code and as I dig more in it I keep finding things. So far I know from debugging on the browser that there is a 'mousedown' event on the document triggered by the second answer element name that shows the options Y/N, then another event 'change' that comes from a div element that boxes the second questions paragraph.

mousedown event 'mousedown' event

change event 'change' event on the right panel are the properties of the argument

Regarding VBA and trying to trigger the event I have tried a lot things now..

.fireEvent "onchange" or "onclick" won't work .iedoc.parentWindow.execScript and the function eventHandle() gives error.

This is the function in JS

elemData.handle = eventHandle = function() {
    // Handle the second event of a trigger and when
    // an event is called after a page has unloaded
    return typeof jQuery !== "undefined" && !jQuery.event.triggered ?
     jQuery.event.handle.apply( eventHandle.elem, arguments ) :
     undefined;
   };

This doesn't give error but nothing happens either:

Dim event_onChange As Object
Set event_onChange = ieDoc.createEvent("HTMLEvents")

While ieApp.Busy Or ieApp.readyState <> 4: DoEvents: Wend
ieDoc.querySelector("select[name='meetingQuestionAnswer(221010)']").Focus
event_onChange.initEvent "mousedown", True, False
ieDoc.querySelector("select[name='meetingQuestionAnswer(221010)']").dispatchEvent event_onChange
    
    
event_onChange.initEvent "change", True, False
ieDoc.querySelector("select.auto-save[name='meetingQuestionAnswer(221010)']").dispatchEvent event_onChange
ieDoc.querySelector("select.auto-save[name='meetingQuestionAnswer(221010)']").Value = "Yes"

the event should be dispatched from the target element or the currentTarget element? or how could I trigger the events to show the Y/N option box and to load the other question?

webdad3
  • 8,893
  • 30
  • 121
  • 223
Pablo G
  • 199
  • 12
  • You can also trigger events by executing the appropriate JavaScript using parentwindow.exescript – QHarr Jan 13 '19 at 10:45
  • I get the error 80020101 that says the action couldn't be completed. I will add the function that handles the change on the question... I've read somewhere that the error on VBA might be because the code in JS for the function has comments... – Pablo G Jan 13 '19 at 10:58
  • There is a typo as well as should be execscript - sorry – QHarr Jan 13 '19 at 12:23
  • 1
    Do you own the web page/javascript code? Or are you trying to drive someone else's interface? If you have javascript skills then I definitely recommend writing a Chrome Extension, https://medium.freecodecamp.org/how-to-create-and-publish-a-chrome-extension-in-20-minutes-6dc8395d7153 I quit scripting IE with VBA a while back once I discovered Chrome Extensions. – S Meaden Jan 13 '19 at 13:26
  • @SMeaden all the work we do is related to information on Excel so I'm more experienced in VBA than JavaScript – Pablo G Jan 13 '19 at 13:48
  • Try to find out which one event handler you actually need to fire by deleting others one by one as described [here](https://stackoverflow.com/a/48674249/2165759). – omegastripes Jan 13 '19 at 15:21

1 Answers1

0

After searching a lot and trying many things on VBA I came to the conclusion that it is not possible to do what I wanted (and if it is is very complex and non convenient) so I decided to turn to Selenium in Python and solved it less than ten lines of code.

Pablo G
  • 199
  • 12