Solved

Help debugging AppleScript to click on webpage link

Posted on 2015-01-04
9
424 Views
Last Modified: 2015-01-13
I am trying to automate the downloading of game records which are stored on successive pages on a website.

I've written the following AppleScript (see below).  It presently correctly opens a sample page (I will try later to add a loop do have this script iterate through multiple pages) but does not succeed in 'clicking' on the 'Select All' link.

I've tried inspecting the element 'Select All' and to use it's id in a Javascript command to automatically click on it (see line:  execute javascript "document.getElementById('a#c17').click()" )  but the element Select All doesn't get 'clicked'.

Since you don't have the user credentials to login to the page in question, I'm attaching a screengrab showing the info for inspect element:  Select All.

I'm not sure how to correctly specify the link 'Select All' in my AppleScript or if I'm using the correct Javascript syntax to click on it.

Thx

Script to Debug:

-- Set Counter
set Counter to 1

-- Set pageURL
set pageURL to "http://www.chess.com/home/my_archive?sortby=&show=live_bullet&color=all&result=all&page=" & Counter
display dialog Counter giving up after 1

-- Open webpage and click on 'Select All' link
tell application "Google Chrome"
      activate
      open location pageURL
      activate
      execute javascript "document.getElementById('a#c17').click()"
end tell
Inspection-of-element-SelectAll.png
0
Comment
Question by:qeng
  • 6
  • 3
9 Comments
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 40531363
provide only the name of the id
"document.getElementById('c17').click()"
0
 

Author Comment

by:qeng
ID: 40532386
Kyle, thanks for your reply.  Unfortunately I still get a 'missing value' error.  See script's events and replies below:

tell application "AppleScript Editor"
      display dialog 26 giving up after 1
            --> {button returned:"", gave up:true}
end tell
tell application "Google Chrome"
      activate
      open location "http://www.chess.com/home/my_archive?sortby=&show=live_bullet&color=white&result=won&page=26"
      activate
      activate
      open location "http://www.chess.com/home/my_archive?sortby=&show=live_bullet&color=white&result=won&page=26"
      activate
      execute current application javascript "document.getElementById('c17').click()"
            --> missing value
end tell
Result:
missing value
0
 

Author Comment

by:qeng
ID: 40532405
I had read somewhere that Google Chrome doesn't support AppleScript drive Javascript ... unsure if that is true.  In any case, I don't have any better success running the following AppleScript using Safari (although changing 'execute' to 'do' for Safari seems to run without the missing value error, though the 'Select All' link still doesn't get activated on the webpage:

-- Set Counter
set Counter to 26

-- Set pageURL
set pageURL to "http://www.chess.com/home/my_archive?sortby=&show=live_bullet&color=white&result=won&page=" & Counter
display dialog Counter giving up after 1

-- Open webpage and click on Select All link
tell application "Safari"
      activate
      open location pageURL
      activate
      delay 5
      do JavaScript "document.getElementById('c17').click()"
end tell

The script's events and replies are as follows:

tell application "AppleScript Editor"
      display dialog 26 giving up after 1
            --> {button returned:"", gave up:true}
end tell
tell application "Safari"
      activate
      open location "http://www.chess.com/home/my_archive?sortby=&show=live_bullet&color=white&result=won&page=26"
      activate
      do JavaScript "document.getElementById('c17').click()"
end tell
0
 

Author Comment

by:qeng
ID: 40536702
Even getting a script which will work in google chrome to target a click on a link would be a start.  We could then see if it can be adapted to work in the situation I'm trying to get working.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 25

Assisted Solution

by:Kyle Hamilton
Kyle Hamilton earned 500 total points
ID: 40536977
say you have an element:

<a href="#" id="someId">a link</a>

Open in new window


the following line triggers a click on the element.

document.getElementById('someId').click()

Open in new window


This works in chrome.
0
 

Author Comment

by:qeng
ID: 40538327
Kyle, still not working. I can get the script to run in Safari but it generates a Missing Value error.  With Chrome, the script generates a syntax error.  

As an aside which is the 'correct' syntax for Chrome (I've tried all three; none are working):

a)  document.getElementById('c17').click()
b)  do JavaScript "document.getElementById('c17').click()"
c)  execute Javascript "document.getElementById('c17').click()"

See attached screen grabs for the Safari and Chrome errors.

Could this be an AppleScript, OS, or Browser version issue?

I run AS Version 2.4.3 (131.2) , OS X Lion 10.7.5 (11G63), Chrome Version 39.0.2171.95 (64-bit)
Safari-works-but-MissingValue.png
Chrome-Syntax-Error-w-Do.png
Chrome-Syntax-Error-w-Execute.png
0
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 40538369
hi qeng,

you should request attention for your question. i'm not able to help you with the apple script piece
0
 

Accepted Solution

by:
qeng earned 0 total points
ID: 40538514
Thanks for getting back Kyle.

I was able to get a script working in Safari by changing the line:

do JavaScript "document.getElementById('c17').click()"

to

do JavaScript "document.getElementById('c17').click()" in current tab of first window

I'll work with that for now.
0
 

Author Closing Comment

by:qeng
ID: 40546211
Wasn't able to get script working in Chrome but managed to eventually get my own script working in Safari.  I don't see how to assign partial points for solutions.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now