0

I am trying to click a button based on the fact that it is starting with the id "submitButton_" given that what comes afterwards keeps on changing (e.g. "submitButton_10a313c673cee"). This is what the button looks like:

<a href="#" class="btn btn-default" role="button" id="submitButton_10a313c673cee">Continue</a>

using the following code, I can click the button:

tell application "Safari" 
    do JavaScript "$(id='submitButton_10a313c673cee').click();" in document 1
end tell 

Therefore, from other questions (e.g. jquery selector for id starts with specific text) I thought I could click it using:

do JavaScript "$(id^='submitButton_').click();" in document 1

However, this does not work. I also tried multiple permutations all of which didn't work, e.g.:

do JavaScript "$('[id^=submitButton_]').click();" in document 1

Any help would be highly appreciated.

  • 1
    Last one seems to work on Chrome. http://jsfiddle.net/Lhmvh8ms/1/ – Ivar Nov 02 '17 at 22:37
  • Are you typing *exactly* what you say you've tried, literally the whole of `do JavaScript "$('[id^=submitButton_]').click();" in document 1` including the `do JavaScript` and surrounding the jQuery portion with quotes? – David Thomas Nov 02 '17 at 22:49
  • @DavidThomas I typed exactly as posted. I have now added to my question that I am running this in applescript for Safari. – Kilgore Trout Nov 02 '17 at 23:20
  • @user3439894 Unfortunately, I can't. One has to login to this database. – Kilgore Trout Nov 03 '17 at 00:14
  • Have you tried `do JavaScript "document.getElementsByClassName('btw btn-default')[0].click();" in document 1`, assuming this is the only or first `class="btw btn-default"` on the page? – user3439894 Nov 03 '17 at 00:17
  • @user3439894 Thanks! I had tried that. Unfortunately, it's not the first (and I couldn't figure out the how many th it is) – Kilgore Trout Nov 03 '17 at 00:25
  • Is it the only button on the page that says "Continue"? – user3439894 Nov 03 '17 at 00:33
  • @user3439894 Yes, and I have tried to use that. Unfortunately, I couldn't figure out how. Thank you so much for your effort. – Kilgore Trout Nov 03 '17 at 00:40

3 Answers3

0

If i understood correctly, you can do it like below

$("[id^=submitButton_]").trigger('click');
  • It gives me {TypeError: null is not an object (evaluating '$('[id^=submitButton_]').click')} – Kilgore Trout Nov 02 '17 at 23:14
  • Also, I am sorry but I am confused about the whole quotes thing. When I run your proposed code it gives me: **Syntax Error Expected end of line, etc. but found “[”.** So I have to replace the " with ' for it to run... – Kilgore Trout Nov 02 '17 at 23:24
  • Maybe you need to add your question tags "applescript" i didnt get the question totally. I will still keep the response here though that maybe you would need it later with your scripting language – Volkan Seçkin Akbayır Nov 02 '17 at 23:30
  • Sorry, I added it. I am new to the game and learning. Thanks for the support – Kilgore Trout Nov 02 '17 at 23:34
0

Edit -- Based on comments about quotes AND using apple script...

Try this:

do JavaScript "$(\"[id^='submitButton_']\").click();" in document 1
samuelnj
  • 1,627
  • 1
  • 10
  • 19
0

I'm not a JavaScript programmer but do use a bit of JavaScript from time to time, e.g.:

do JavaScript "document.getElementsByClassName('btn btn-default')[n].click();"

Where n in [n].click() is set to the index number, to click buttons by their class name on various web pages.

The following example AppleScript code is what I used to get the value of n in [n].click():

Set the two variables as appropriate and the remaining code is tokenized.

In your case, e.g:

set theClassName to "btn btn-default"
set theText to "Continue"

tell application "Safari"
    tell document 1 of front window
        set theCount to (do JavaScript "document.getElementsByClassName('" & theClassName & "').length;") - 1 as integer
        repeat with i from 0 to theCount
            set innerText to do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & i & "].innerText;"
            if innerText is theText then
                do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & i & "].click();"
                exit repeat
            end if
        end repeat
    end tell
end tell

I ran the above in Script Editor on a page that had many elements with the btn btn-default class name and with one having Continue as its inner text. The last event was:

do JavaScript "document.getElementsByClassName('btn btn-default')[2].click();"

So, if you do the same, you should be able to find out the proper index number and use it to click the button.

Note that while the example AppleScript code could be used as the normal code, it's just meant to get the index number and see that it actually clicks the button so you can use the single line of code in the actual script.

user3439894
  • 7,266
  • 3
  • 17
  • 28