Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

JavaScript to externally automate Webpage operation

I am a newby to JavaScript, suggested to me to use to automate the task of opening of a Web page, selecting three internal buttons in sequence to download the underlying chart data. I have created the App via Automator on macOS, to run the Script, successfully open the Web Page, but cannot find a way to select and click() on the buttons. Can someone please help me. Robert.

This is the code suggested by Grok 3 Beta, but I see this error: Error: First parameter passed to Document Constructor must be an object.

function run(input, parameters) {
var Safari = Application('Safari');
Safari.activate();

// Open the AEMO data dashboard (Grok 3 Beta recomendation opens the web page correctly) Safari.Document().make(); Safari.windows[0].currentTab.url = 'https://www.aemo.com.au/energy-systems/electricity/national-electricity-market-nem/data-nem/data-dashboard-nem'; delay(10); // Wait for page to load

// Click the Fuel Mix tab (target the active <li> in the tabs) Safari.Document(0).doJavaScript("document.querySelector('.tabs .active').click()"); delay(5); // Wait for tab content to load

// Select 48 hrs from the dropdown Safari.Document(0).doJavaScript("document.querySelector('#interval').value = '48H'; document.querySelector('#interval').dispatchEvent(new Event('change'))"); delay(5); // Wait for selection to take effect

// Click the download button Safari.Document(0).doJavaScript("document.querySelector('.visualisation-icon-button').click()");

return input;

}

Where is the error? You haven't formatted the code properly, so it's difficult to read.

You need to use the code formatting tool in the tool bar when you create a post. Once you've made your post you should always read it and ensure it looks correct. Yours obviously is not correct.

Could you reformat it so that all the code is within the three backticks, please? And then tell us which line you're seeing the error.

(As an aside, every time someone comes on these forums with a problem that's been created by an 'AI' tool it's because the 'AI' doesn't work and has led them down the wrong route. Why people keep relying on them is beyond me. You should learn these things for yourself. If everyone keeps resorting to 'AI' nothing new will ever be created.)

Thank you DarkPaw, for responding so promptly. And for the reprimand, justly deserved. Oops, was not aware the (3) black ticks were so important. The error (line 11, and in the alternative code in line 13) occurs at the first attempt to find the "Fuel Mix" button. Error: "First parameter passed to Document constructor must be an object"

function run(input, parameters) {
    var Safari = Application('Safari');
    Safari.activate();
// Open the AEMO data dashboard (Opens the web page correctly) 
    Safari.Document().make();
    Safari.windows[0].currentTab.url = 'https://www.aemo.com.au/energy-systems/electricity/national-electricity-market-nem/data-nem/data-dashboard-nem';
//  Open the AEMO data dashboard (Fails to open the Web page consistently)
//  Safari.openLocation('https://www.aemo.com.au/energy-systems/electricity/national-electricity-market-nem/data-nem/data-dashboard-nem');
    delay(10); // Wait for page to load
//  Click the Fuel Mix tab (target the active <li> in the tabs) (Creates an Error: First parameter passed to Document constructor must be an object)
//    Safari.Document(0).doJavaScript("document.querySelector('.tabs .active').click()");
//  An Alternative: Click the Fuel Mix tab (fourth tab in the list, creates the Error)
	document.querySelectorAll('.tabs-heading li').length;
	const tabs = document.querySelectorAll('.tabs-heading li');
	if (tabs.length > 3) {
	  tabs[3].click();
	} else 
	console.error('Not enough tab elements found.');
	}
    delay(5); // Wait for tab content to load
//  Select 48 hrs from the dropdown    Safari.Document(0).doJavaScript("document.querySelector('#interval').value = '48H'; document.querySelector('#interval').dispatchEvent(new Event('change'))");
    delay(5); // Wait for selection to take effect
//  Click the download button
   Safari.Document(0).doJavaScript("document.querySelector('.visualisation-icon-button').click()");    
    return input;
}

Thanks again for your patience. Robert.

JavaScript to externally automate Webpage operation
 
 
Q