Statement Download part 2

Ok I am now working on a different area of the script. I was trying to select the text “Online Statement Ready to View” but I couldn’t get Javascript to find it and click it. So after doing a lot of inspection I found a .jpg on the page and after much trial and error I was able to get it to click on “Statements” which takes me to the current months statement along with a list of the others in the right frame. So this is a different path to the same result.

Using the click image step I noted that the mouse was resting after the step ran and I could see where it landed. I kept adjusting the numbers as it was landing lower then the link location. I finally got within 5 numbers. (I assume these are pixels), and I nailed it.

Two things I learned from this:
1.) It wasn’t finding the image. So by adjusting the focus on the image the KM alert that it couldn’t find the image went away. This gave me a hint that maybe now it was finding the image. So I was able to move on to getting it to click at (250,330).
2.) Peter often suggests breaking macros into smaller steps. (Don’t quote me on that actual wording) So that’s what I did here. I used F2 to run the first macro that logs me into the account then navigates to the statements page. Then it fails when trying to click the view statements. So I continued on in a new macro and assigned it to F3 which allowed me to work forward from where it failed. Once I got all that working I now only have the final issue of getting it to click the print button, then save as pdf, then title the pdf.

So at this point I am wondering if I should just use a "click at (0,0) action to try and nail the link with a click using the image as before (the "click image at (250,330) step above), only now using the adjusted coordinates for “PrintVersion” link to print the statement? (Then I will use keystrokes to fire my Print PDF keystroke I programmed into MacOSX, which is “^P” when the print dialogue is open).

[quote=“Tunes, post:21, topic:2513”]
I was trying to select the text “Online Statement Ready to View” but I couldn’t get Javascript to find it and click it.[/quote]

Hey John,

You did use Keyboard Maestro link pop-up menu on that page to try to find the correct link – yes?

-Chris

@ccstone, Hmm… Gee. I don’t know. What ’ link pop-up menu’ are you referring to? Is this something in the Service Menu that I missed or maybe didn’t install?

Hey John,

See the little v after Safari (by the cursor)? That indicates a pop-up menu.

Using that you can discover right quick if Keyboard Maestro can see a link on a page.

The displayed links are from: https://www.google.com

-Chris

@ccstone
Chris,
I think the reason I missed that little popup menu is because I was looking at the Execute JavaScript panel.
However when I went to the Click Safari Link panel and clicked on the Safari Popup menu ("v"), it says:

"No links found"

So I see what the problem is now, and none of the previous attempts at JavaScript calls worked.

As I mentioned before the page where I am trying to set the link is several frames so I wonder if that's causing a problem. I was also curious if it's possible they might have some security in place which blocks my using JavaScript? (Although it's more likely I just don't have the right element defined yet).

It looks like they are calling a app number then in the same argument specifying a document title ID.
This is what Inspect Element in Safari snags for me:

<a href="javascript:openLink('/cowww5/viewers/viewdoc_titlejs.asp?DoctitleID=1301227&AppID=128')">Statements</a>

I tried using "Statements" by itself which didn't work. I also tried Execute JavaScript javascript:openLink('/cowww5/viewers/viewdoc_titlejs.asp?DoctitleID=1301227&AppID=128') but I can't recall exactly what I tried. Taking the code you came up with the login page I used similar language and plugged in the line above.

I tried:

 `document.forms["form1"]["loginInput"]`

(I used document.forms but I am not sure how)
and also:

document.getElementById('btn-login').click()

(I used document.getElementById('btn-login').click() and inserted the stuff between the parenthesis but realized that was probably looking for a button which doesn't exist in the link referenced above)

And finally I noted the following:

So I tried this:

Which didn't work. So maybe I lack the needed JavaScript vocabulary to do this.

Hey John,

There's one in the forms actions too. (Always look for and try the pop-up menus in the actions.)

That can happen.

I would look at the target page URL and compare it to this:

open Link('cowww5/viewers/viewdoc_titlejs.asp?DoctitIe|D=1301227&App|D=128')

You may be able to decode how they're calling the page.

You may or may not be able to extract that call from the document source.

document.body.parentNode.outerHTML

-Chris

@ccstone
Here's what I have for the target page URL:

https://estatements.cowww.com/cowww5/viewers/viewdoc_titlejs.asp?DoctitleID=1301227&AppID=128

So I plugged that into my actions list with "Open URL" which works to get me to that page except it opens in a new tab. I would have preferred it open in the same window but it will do for now.

Yes I did try that but your inclusion of "document.body.parentNode.outerHTML" makes me wonder if I am missing something you're trying to point out. What does that refer to?

Here is the source from the page. I don't think it will help much unless there's something I overlooked.
I have uploaded it below.
BankStatementsSource.txt.zip (2.0 KB)

Hey John,

How did you acquire this? By clicking the link?

You don't want to use an Open a URL action – you want to use a Set Safari URL action.

I'm just showing you how to get the document source for parsing.

I usually do it all in AppleScript, because I can use the Satimage.osax AppleScript Extension's regular expressions. I'm used to them, and they're a little more flexible than Keyboard Maestro's.

A fairly complex macro can be reduced to this AppleScript:

set safariSource to doJavaScriptInSafari("document.body.parentNode.outerHTML")
set foundText to fnd(".*https://estatements.*", safariSource, true, true) of me

------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------
on fnd(_find, _data, _all, strRslt)
  try
    find text _find in _data all occurrences _all string result strRslt with regexp without case sensitive
  on error
    return false
  end try
end fnd
------------------------------------------------------------
on doJavaScriptInSafari(javascriptStr)
  try
    tell application "Safari" to do JavaScript javascriptStr in front document
  on error e
    error "Error in handler doJavaScriptInSafari() of library NLb!" & return & return & e
  end try
end doJavaScriptInSafari
------------------------------------------------------------

On my system the handlers are hidden in a library, so the main script is only 2 lines of code.

This is way faster than iterating through all the lines and searching each one with Keyboard Maestro's regex.

Okay, so we parse that source:

set foundText to fnd("<A HREF=\"javascript:openLink(?>(?!A HREF).)+>Statements</A>", safariSource, true, true) of me
if length of foundText = 1 then
  set foundText to item 1 of foundText
end if

--> <A HREF=\"javascript:openLink('/cowww5/viewers/viewdoc_titlejs.asp?DoctitleID=1301227&AppID=128')\">Statements</A>

* The result is the line starting with -->

Compare the result with the URL from the object page:

<A HREF=\"javascript:openLink('/cowww5/viewers/viewdoc_titlejs.asp?DoctitleID=1301227&AppID=128')\">Statements</A>

https://estatements.cowww.com/cowww5/viewers/viewdoc_titlejs.asp?DoctitleID=1301227&AppID=128

The JavaScript call has the component parts of the of the target page, so all you have to do is extract and assemble them – then load your new URL in the current page.

In summation:

  1. Instead of trying to click the statement link in the original page we parse its source for the JavaScript call.
  • We extract the relevant bits from the JavaScript call and assemble the proper target URL.
  • We load the target URL in the front Safari page.

On to the next step.

Hopefully this is reasonably clear.

-Chris

1 Like

Yes, I clicked on "Statements" and copied the resulting link from the resulting page.

Got it. Changed it and voila!

I am going to have to study this a bit and see if I can sort out exactly what's happening. :confused:
Thanks again Chris!

Hey John,

Keep in mind that this line is just an example and isn't pertinent to your problem.

What I'm doing in the code you quoted is:

  1. Getting the source of the front page in Safari.
  • Parsing it for a regular expression.

The handler is a wrapper for this line, which is a function from the Satimage.osax.

find text _find in _data all occurrences _all string result strRslt with regexp without case sensitive

-Chris

Just reporting back on my efforts on this one. I am still working on this one. In the mean time I have gotten most of it to work by using “click on found image” and setting the blur amount to 50%.

Since I got it working I finished up some final steps I wanted to add to snag the date in the Satement. I inserted a double click action and some [OPTION] + [SHIFT] + [ARROW LEFT] to get the date of the statement and copy it to the clipboard and paste it when it’s needed later when I save the PDF.

It clicks on the image then I use “Type Keystrokes” to [COMMAND]+[P] to print and in the print dialogue. I have [COMMAND]+[P] defined as a custom finder keyboard shortcut to print a PDF. So I just hit [COMMAND]+[P] again in the Macro to save it as a PDF.

When the “Open & Save” dialog window appears I use [COMMAND] + [OPTION] + [L] to move to the downloads folder and then paste the clipboard contents using the “Type Keystroke” action followed by another “Type Keystroke” action to press the return key.

This is a pretty dumb methodology but it will have to suffice until I can get my JavaScript and RegEx chops up to speed. Both of which are interdependent which makes it so hard to move up a tier coding.

Done for the next version.

1 Like