How to expand all collapsible sections on a web page for text search

Hello everyone,

I have been using an internal website with multiple pages that are designed to store information under collapsible sections. In order to find what I look for, I need to expand all sections to perform a Cmd+f text search.

Chrome’s “Inspect element” feature indicates that collapse triangles are tied to a “class=collapsed” item.

I would like to create a script that expand all collapsed text on a web page.

Any help would be much appreciated.

Regards,

I’m guessing this is going to be difficult for us to help you with without an example page.

Also, the solution would be Javascript; you’d probably have more luck getting an answer on SuperUser or somewhere.

If it were me, I’d try grabbing the page source and just run the search on that.

I don’t know what you’re doing with the text you’re searching for. But something like this:

tell application "Google Chrome"
	set source to execute front window's active tab javascript "document.documentElement.outerHTML"
	if source contains "whatever" then return true
end tell[/AppleScript]

Maybe that's not going to work for you. If you really do need to expand the page, I suspect it's possible, but again, hard for us to work on with no sample page.

Thank you @frederik71 and @t.spoon for your responses.
Unfortunately, I do not have control over editing page coding and since it is an internal page I can not share it.
Therefore the only option for me seems to locate page elements via a script command and perform a click action for all.
However, to elaborate more

https://support.apple.com/guide/iphone/welcome/15.0/ios

This page has a quite similar strucure.

If you click “table of contents” you would see a pop-up list that contains multiple collapsible items.

What I need to do is to expand all collapsed items at once with a script.

Regards,

This page has links. Move the mouse over each link, perform right click to copy its URL. No, you can open this “collapsable” thing which is link using open location command:

For example, link Table of Contents has URL “https://support.apple.com/en-gb/guide/iphone/toc/15.0”.

So, simply open this URL:


open location "https://support.apple.com/en-gb/guide/iphone/toc/15.0"

Opening URL of link iPhone 13 mini:


open location "https://support.apple.com/en-gb/guide/iphone/iph7d116e557/15.0/ios/15.0"

You can get all embedded links b programatically[/b] as well. Open webpage, run script:


set theDocumentLinks to "function documentLinks() {
//
var arr = [], links = document.links;
for(var i = 0; i < links.length; i++) {
	arr.push(links[i].href);
}
return arr
}
//
documentLinks()
"

tell application "Safari" to set theLinks to do JavaScript theDocumentLinks in document 1

Now, you can open this links one by one. Or, use repeat loop. NOTE: if the page has too many embedded URLs, opening all of them in the loop is bad idea. This will overflow the browser cash.

Thank you @KniazidisR

However, I am not trying to open the URL of “Table of contents”.

When you click that you will see a pop-up window

What I need to do is to expand all collapsed items at once on such a page.

For instance, this list has a “Set up and get started” collapsed item and under this item, there is an “Apple ID and iCloud” collapsed item.

I want Applescript to detect all collapsed items and expand all at once so that I can perform a Cmd+f search, since without expanding all collapsed items a Cmd+f search with “Apple id” would return “0”.

Regards,

Open manually the Table of Contents, run following script.


tell application "Safari"
	activate
	tell document 1
		do JavaScript ("document.getElementsByClassName('section').length")
		set sectionsCount to result as integer
		repeat with i from 0 to (sectionsCount - 1)
			do JavaScript ("document.getElementsByClassName('section')[" & i & "].click();")
		end repeat
	end tell
end tell

Thank you @KniazidisR that works like a charm after I adapted the class name.

Kindest regards,