[Bank Statement Download Script] Set link or perform JavaScript?

@JMichaelTX

Thanks JM. What you have is exactly what it was.

So I copied it using chrome and got:

//*[@id="ctl00_MainContent_ucEStatements_dlStatements_ctl00_lbStatementDate"]

Pasted it as:

ctl00_MainContent_ucEStatements_dlStatements_ctl00_lbStatementDate

Then I copied your JS as:

clickOnLink(document.kmvar.xPath);

function clickOnLink (pXPathStr) {

var elemFound = document.evaluate(
pXPathStr, document, null, 0, null
).IterateNext();

if (elemFound) {
elemFound.click();
}
else {
alert('Element NOT FOUND for XPath:\n' + pXPathStr);
}

};

It didn't work. So a couple of questions. I used returns to format the above JS and get it to look like yours. I assume it's ok to do that unless I need to escape some characters, etc. I also assume I close each statement with " ; " and that bracketed statements require curly braces, is that correct?

You are correct. Everything you have is fine, although technically you don’t need the final semi-colon.

Here’s a reformatted version, which might help you see the logic a little clearer:

clickOnLink(document.kmvar.xPath);

function clickOnLink(pXPathStr) {
    var elemFound = document.evaluate(pXPathStr, document, null, 0, null).IterateNext();

    if (elemFound) {
        elemFound.click();
    } else {
        alert('Element NOT FOUND for XPath:\n' + pXPathStr);
    }
}

No. That is not correct.
The XPath value is exactly what you copied. It should be pasted as is into the KM Set Variable Block:
//*[@id="ctl00_MainContent_ucEStatements_dlStatements_ctl00_lbStatementDate"]

There is no need to copy, nor change, the JavaScript.
It is already styled properly in the JavaScript for Browser action.
If you want to use it in another macro, then just copy the KM JavaScript in Safari Action. It is advised to first test code from others as is before making changes.

As a test, just change the "Set Variable" Action in my above macro, and then test it on your web page.

Thanks Dan, Are you using Atom or ?
Really starting to look hard and making learning JavaScript top priority. It's everywhere and very handy to use in Filemaker Pro.

Along with what I pasted I also tried what you listed…
(//*[@id="ctl00_MainContent_ucEStatements_dlStatements_ctl00_lbStatementDate"]).

I also tried single quotes thinking maybe that's why it wasn't working. I will take another look at it. Probably some silly error on my part.

Don't change anything in the XPath that you copy using the Chrome tool.
Just copy it, and paste directly into the KM Action Set Variable to Text.

I just posted a more complete macro:

Yes, I use Atom. There’s a topic here somewhere on how to set it up.

@JMichaelTX
No joy. I also tried the new macro you posted. Now here’s the funny thing. I went back to chrome and copied the xPath again. I compared it with what I had and for grins pasted it again. Then I ran my macro and this time I got a pop-up error stating that the xPath: blah, blah, blah couldn’t be found. It listed the xPath I pasted. Then I ran it again and the error dialog did not re-appear. So it appears it tried to run at least that one time which leads me to believe there’s a problem with the xPath I copied from Chrome.

Can you please just use my macro until we get the XPath to work. We need to keep things simple, and not introduce any other variables.

If you are getting a XPath of the right element, and it is a anchor element (<a href="blah blah">displayed text</a>) then it should work.

Can you do a video of the process you use to get/copy the XPath, starting with the right-click on the web page element?

Sure. Here it is…

Thanks, but that is way too small to read.
It would be much better if you can post the actual video (instead of an animated GIF), so I can stop, replay, and study the process.

Also, please post the XPath that you copied.
You will need to put it between back quotes so the forum editor will display properly.

Ok, here's the xPath...
//*[@id="ctl00_MainContent_ucEStatements_dlStatements_ctl00_lbStatementDate"]

And just in case you want to see it with the outer element...

<a id="ctl00_MainContent_ucEStatements_dlStatements_ctl00_lbStatementDate" href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$MainContent$ucEStatements$dlStatements$ctl00$lbStatementDate", "", true, "", "", false, true))'><i class="fa fa-file-pdf-o"></i>10/10/2016</a>

What happens when you manually click on the link?
Do you get a popup, file download, or what?

What happens when you trigger my macro with the web page frontmost?

Do you know how to use the JavaScript console in the Chrome Developers tool?

If so, you might try this, entering one line at a time, and then RETURN

var linkElem = document.getElementById("ctl00_MainContent_ucEStatements_dlStatements_ctl00_lbStatementDate")
linkElem    // should show the anchor element
linkElem.click()   // should click the link

Let me know what happens.

1 Like

Please see YouTube Video showing how to get XPath, and then demo of the above macro.

@JMichaelTX Thanks, I will test the above suggestions and get back to you.

I am a little confused on your video post because my short clip shows exactly the same thing as your video. Shall I assume that you posted the video for others?

Since I could not read your animated GIF (too small), I posted a video that is for everyone's benefit. I wanted everyone to see that the macro will definitely work, if setup correctly. If you did exactly what is in my video, then great! That's one less issue to deal with.

Aah!. I understand. Try this: Control click on the GIF, select “open image in new window”, hold COMMAND and press the “+” key several times. Alternately you can just COMMAND + "++ repeatedly to view something then COMMAND “-” (minus) to go back to the original size. Another option is to hold CONTROL and use two fingers to slide up on the track pad which will zoom in on the GIF (if you have a track pad setup the right way).

Maybe not. I'm very familiar with how to enlarge an image on a page (which I had already done).
But doing so in this case results in a very blurred image, still hard to read.
IMO, it is best to post the actual video in cases like this.

But at this point it is irrelevant. I have posted my video on how to get an XPath that you can follow.