Clicking Link in Safari

I am pulling data from a web site that has a predictable layout that I can parse with BBEdit. There are 20 entries per page and 10 pages of data accessible via a “next” link. There are various other links on the page (static ads, GUI, etc.).

I want to run the script, have it parse the 10 entries, click the “next” link, and repeat. Is there any way to specify link clicks in Safari, outside of UI scripting? Or maybe with UI scripting if it’s repeatable?

Thanks in advance,

Hi Kevin,

if you use Safari, take a look at its javascript capabilities

http://www.apple.com/applescript/safari/jscript.01.html

Hi Kevin,

Also, you can try searching here for javascript Safari. There are several posts on how to get a link by name (“next”). Once you have the index of the link though, you don’t need to search for it in the script.

gl,

I have had the same problem, which I thought to solve with GUI scripting. However the webpages I was working with changed a lot, so GUI scripting turned out useless. With the help of a few code snippets I found on this forum, I put together a script (using javascript) that works ok for me. :slight_smile:
Is this any help?

 
--the text on the link
set link_list to {"Link1", "Link2", "Link3", "Link4"}

--get the links in the list one by one & find them on the consecutive webpages
repeat with n from 1 to number of items in link_list
	set the_wanted_link to item n of link_list
	my finding_the_link()
	my page_loaded(200)
end repeat

--look for the_wanted_link on the webpage
on finding_the_link()
	tell application "Safari"
		set counter to 0
		--  find the number of links in the front document
		set link_count to do JavaScript "document.links.length" in document 1
		delay 0.5
		repeat link_count times
			set counter to counter + 1
			--select the correct one
			set the_link to do JavaScript "document.links[" & counter & "].innerHTML" in document 1
			if the_link contains the_wanted_link then
				set go_link to do JavaScript "document.links[" & counter & "].href" in document 1
				exit repeat
			end if
		end repeat
		delay 1
		set the URL of document 1 to go_link
	end tell
end finding_the_link

--webpage has to be loaded before moving on
on page_loaded(timeout_value)
	delay 2
	repeat with i from 1 to the timeout_value
		tell application "Safari"
			if (do JavaScript "document.readyState" in document 1) is "complete" then
				return true
			else if i is the timeout_value then
				return false
			else
				delay 1
			end if
		end tell
	end repeat
	return false
end page_loaded