0

I want to get all the number from a website , which are in the same row.

the all have the same Class :

<td class="sortable dsNumberId">

                                    10999694994

                            </td>

I would like to return this to the clipboard and have a result like this :

10999694994

10949644992

10959594991

109796976

The number of value to collect is random, it can from 2 to 100 and the first class="sortable dsNumberId" is actually invalid (just the title with a link)

I have this script here :

tell application "Google Chrome"
    tell tab 2 of window 1 to set DSIDsList to execute javascript "var outPut=[0]; var arr=document.getElementsByClassName('sortable dsPersonId');for (var i in arr) {outPut.push(arr[i].innerHTML)};outPut;"

end tell

The issue is at the end which give me a text " ", missing value, missing value, missing value}"

and at the beginning which give me the invalid title / URL which I don't need.

1 Answer 1

1
tell application "Google Chrome"
    tell tab 1 of window 1 to set DSIDsList to execute javascript "
    var outPut=[]; 
    var arr=document.querySelectorAll('.dsNumberId');
    for (var i=0; i < arr.length; i++) {
        if(arr[i].innerHTML !== ''){
            outPut.push(arr[i].innerHTML.trim())
        }
    };
    outPut;"
end tell

Change the search to use document.querySelectorAll, which enables you to target a single classname, such as dsNumberId. (Also note your script was looking for dsPersonId while your sample HTML specified dsNumberId.) I also added a trim() to remove extra whitespace from the returned results.

Given this HTML (note the extra white space as in the original post),

<html>
<body>
<table>
<td class="sortable dsNumberId">

                        20

                </td>
                <td class="sortable dsNumberId">

                        30

                </td>
                <td class="sortable dsNumberId">

                        40

                </td>
                <td class="sortable dsNumberId">

                        50

                </td>
                <td class="sortable dsNumberId">

                        60

                </td>
                <td class="sortable dsNumberId">

                        70

                </td>
                </table>
                </body>
                </html>

the revised script will return

{"20", "30", "40", "50", "60", "70"}
Sign up to request clarification or add additional context in comments.

2 Comments

That's great thank you, I added this at the end "set the clipboard to items 2 thru -1 of DSIDsList as string" otherwise I keep having the URL in the first entry. however the only issue is the result in the clipboard is set to 203040506070 instead of 20 30 40 50 60 70, how can I set the clipboard to the results with space in between ?
NP, glad it helped. For adding space, you can tell AppleScript which delimiter to use when converting a List to a String. Check the answer on this post, it has a nice concise way of handling it. stackoverflow.com/questions/37289223/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.