Trying to streamline some code that gets values from a table on the web by downloading the table rather than getting the value directly from the web as some times the table on the web is quite large. The following script certainly gets the table as it can count its rows and columns. I can and do get the results I need by scrolling through the table online, I was hoping to improve on that . Thanks for any suggestions.
Thanks for getting back to me . The source for what I am doing is finance.yahoo and this is a URL for a stock Federal Signal Corporation (FSS) Options Chain - Yahoo Finance. that should bring up the website for “Federal Signal” and you will see in the bottom half two tables and as you can see from my code I am accessing the 1st one, hope this helps
property header : {}
property mytable : {}
tell application "System Events"
tell application process "Safari"
set ty to table 1 of group 22 of group 1 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window 1
set Ry to count rows of ty
set Cy to count columns of ty
repeat with C from 1 to Cy
set end of header to (value of UI element 1 of UI element C of row 1 of ty)
end repeat
repeat with R from 2 to Ry
set tmpRec to {}
repeat with C from 1 to Cy
if (role of UI element 1 of UI element C of row R of ty) = "AXLink" then
set end of tmpRec to (title of UI element 1 of UI element C of row R of ty)
else
set end of tmpRec to (value of UI element 1 of UI element C of row R of ty)
end if
end repeat
set end of mytable to tmpRec
end repeat
end tell
end tell
Thank you the correct group is 24 not 22, and I apologise for not posting my code correctly , I am sorry but I just have not been able to figure that out since the site changed
Weird, I cant get group 24 to work, but 22 works fine
Is it the “PUTS” table or the “CALL” table?
Also please learn how to post code so that the “Open in Script Editor” button shows up and also the code doesn’t get it’s quote character replaced with typographer quotes.
tell application "Google Chrome"
set js to "result='';
var t = document.getElementsByTagName('table');
var rows=t[0].querySelectorAll('tbody>tr');
[...rows].forEach(r => {
const cells=r.getElementsByTagName('td');
result += [...cells].map(c => c.innerText).join(',')
});
result;
"
tell tab 1 of window 1 to set result to execute javascript (js as string)
log result
end tell
It injects JavaScript into the browser (using execute). The JavaScript uses the obvious DOM methods to get all cells of the first table (t[0]) by first retrieving all table rows in the table body (querySelectorAll('tbody > tr')) and then looping over the rows and getting the innerText of all their td elements.
These innerText values are returned as a single string, separated by commas.
This is obviously a completely different approach from yours, but it is what one does in general to scrape web content. Using UI scripting is error-prone and might fail with currently invisible stuff, as you found out.
To post code, you should include it in three backquotes like so:
```applescript
code goes here
```
I’m not sure if the “applescript” language identifier is needed here, but including it shouldn’t do any harm.
To have the same thing happen in Safari, use tell application "Safari" at the top and set result to do JavaScript js in tab 1 of window 1 instead of the line tell tab 1…
It is not possible to do all this with Firefox since its scripting support is still in its infancy – since over 20 years.
At some point for me group 22 became group 23. I guess it takes a while for everything to finish loading. With group 23, I ended up with a list of the three records in the Calls table.