I'm running an Excel VBA macro that collects information from this webpage. To get the pricing information to appear, you need to click on the box labeled "5 oz. - 4 Count" over on the right side, middle of the page. Once you click on the box a pop-up window appears with the desired information.
My problem is that I have been unable to figure out how to programmatically click on this box.
From examining the page elements, it seems that the following element
<a class="os_opt_dd" href="#"><span class="os_opt_dd_s">5 oz. - 4 Count</span><input tabindex="-1" value="1144"></a>
Set ie = CreateObject("InternetExplorer.Application")
.Visible = True
.Top = 50
.Left = 530
.Height = 400
.Width = 400
For x = 0 To ie.document.all.Length - 1
ie.document.all.Item(x).Click ' x=1101, 1103 and 1190, 1223 often seemed to generate the pop-up
If you follow the
click event stack trace you will discover there are lots of things going on in this webpage. Not least there seem to be three separate tracker scripts which seem to need to hook the DOM events in order to operation. I'd hazard a suggestion that is the cause of your apparent issue.
As of jQuery 1.3, .trigger()ed events bubble up the DOM tree
I don't believe the vba-based code like this:
ie.document.foo.Click - is, in your case, causing event propagation. So let's base a solution on running the necessary jquery
trigger script within your programatically created IE session.
<a> tag (anchor) that you want to automate has a class of
os_opt_dd. There are 13 such anchors on the page (with the same class) and you want the last. There is probably a better way to isolate the anchor (probably based on the parent
<div>) but I'll leave that as an exercise for you.
So the jquery code to provide the automation would be this:
// get the HTML element we want to automate var anchor = $('a.os_opt_dd'); // trigger the mousedown event on this anchor element $(anchor).trigger('mousedown');
As a one-liner it reads:
Note we need to trigger the
mousedown event and not the
click event. This is because (IMO) the event propagation is triggered by the first event of the user action e.g. the pressing of the mouse button on the particular element of the document. If you Google/ search on Stack Overflow then you will find lots of threads/ discussion on this topic of clicks/ events/ jQuery etc which is all useful reading material. If you ask a jquery expert if there's a better way to code that, I fully expect them to come up with something better ;)
Anyway, finally, our excel-vba code will be:
Note I used early binding as it was a pain to use late binding for testing the solution to this problem. The required references are: