Re: HTML parsing--need to be able to click on parsed elements
Re: HTML parsing--need to be able to click on parsed elements
- Subject: Re: HTML parsing--need to be able to click on parsed elements
- From: Axel Luttgens <email@hidden>
- Date: Fri, 10 Jun 2016 12:19:00 +0200
> Le 10 juin 2016 à 05:39, William Dockery a écrit :
>
> […]
>
> However, what I failed to clarify to you and to myself is that I need more than text, or dates. I actually want to CLICK on the first element that matches:
>
> <A href="#"onclick=“refreshSession
>
> This text pattern signifies an HTML element that, when clicked, will download a specific type of document, which is my general objective. Clicking on the first of these elements will download the most recent document, which is my specific objective.
>
> And I can’t just search for <A and click on the first <A element, because there are several other irrelevant <A elements higher up in the source of the page.
>
> […]
Hello William,
Since Safari provides the "do javascript" command, you may try to delegate as much as possible to JavaScript.
For example, assuming that
- the above conditions are sufficient
- the ordering of the DOM elements reflects the ordering of the links as viewed in the browser window
you could try:
set js_code to "
// Let's fetch the document's anchors.
var a = document.getElementsByTagName('a');
// Loop over the anchors.
for (var i = 0; i < a.length; i++)
{
// Skip anchors whose href is not '#'.
if ( a[i].href != '#' ) {
continue;
}
// Skip anchors not having a 'onclick' attribute.
if ( a[i].onclick == null ) {
continue;
}
// Skip anchors whose 'onclick' attribute does not contain the
// exepected call of 'refreshSessionAndPopupWindow'
if ( a[i].onclick.toString().indexOf('refreshSessionAndPopupWindow(') < 0 ) {
continue;
}
// We found a suitable anchor; let's click it.
a[i].click();
// We're done: we are interested in the first suitable anchor only.
break;
}
"
tell application "Safari"
tell front window
tell current tab
do JavaScript js_code
end tell
end tell
end tell
HTH,
Axel
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden