Read & Store partial Safari URL then run script when finished loading

Greetings all. I am writing a script that moves from one webpage to another. Once the page finishes loading I want it to continue running the script. My dilemma is I don’t know what the exact url is. I fill out a form, click “submit” then the next website is a constant url followed by ever changing numbers, then another constant url with random numbers at the end. It is this final url (which generates automatically), that I’m interested in. The final page is the website with the processed information. The site that I’m interested in has the following form: http://www.websitename.com/xx.xxx.x.xx.x. I know how to tell the script to repeat a delay until a website loads if I know the exact url. How can I tell the script to read the url, store it as a variable (if I need this step), then continue running the script once it has finished loading? Thank you in advance for any and all help. Please let me know if you need any more information or for me to try and clarify better.

I noticed one change from when I first posted this question. The middle page finishes loading the webpage very quickly, but it is the creating of the report that shows up on the 3rd page that takes the time. The work flow is: 1st page-fill in information; 2nd page-creates the finished form; and the 3rd page displays the finished form. If I create a repeat function to see if the 2nd page is finished loading it shows that it is finished almost instantly; which doesn’t work for me as the report is still being created. What I think I need to do is check the text of the 2nd page for a specific line (which is always present on this page), and tell the script not to advance while safari still displays text “insert text here.” How can I create a repeating ‘if’ statement that will check the text of the 2nd webpage and not move on until that text is gone, ie: when “insert text here” is gone that means the form is finished and it’s going to move to the 3rd page to display it and my script may continue? Any help or suggestions would be much appreciated. Thank you and have a good night.

Try this.


tell application "Safari"
	set theSource to "insert text here"
	repeat until theSource does not contain "insert text here"
		set theSource to source of document 1
		delay 2
	end repeat
end tell