Skip navigation

Clicking JavaScript element without Name or ID

787 Views 12 Replies Latest reply: Oct 15, 2012 9:34 AM by Queerly RSS
Queerly Level 1 Level 1 (0 points)
Currently Being Moderated
Oct 12, 2012 2:57 AM

Hi, I'm new to AppleScript but learning fast. However, JavaScript is still marked as "there be dragons" for me, though I've made some progress already.


Anyways, I'm facing the follwing conundrum: I managed to write an AppleScript to fill in a Search Querry on a WebPage using JavaScript, however, I can't manage to get JavaScript to do the last step, i.e. press the "Submitt button", as it has no ID or Name associated with it. The button is defined as follows:


<input type=​"submit" value=​"Search...搜索... ">​


I've tried something on the lines of:

--do JavaScript "document.getElementsByTagName('submit')[0].submit()" in document 1




--do JavaScript "document.getElementsByTagName('submit')[0].click()" in document 1


but to no avail.


What should the JavaScript Command look like? Could anybody help?

MacBook Pro, Mac OS X (10.7.4)
  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Oct 12, 2012 6:05 AM (in response to Queerly)

    well, first off, the tag name would be 'input' not 'submit'.  you'd want to try document.getElementsByTagName('input') and search through the resultant nodes for the correct one.  However, it would be helpful to see more of the html.  for instance, if the submit button is inside an html form, you can identify it more easily by finding the form first.

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Oct 12, 2012 11:53 AM (in response to Queerly)

    Actually, there is an easier way.  The submit button is within a FORM element with the name 'com'.  As such, you don't even need to click the button.  Just submit the form programmatically using this code:




    And you need to get control of your auto-correct.  I mean seariously: "Accidentally, is there any cleaverer way..."? 

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Oct 13, 2012 7:27 PM (in response to Queerly)

    Queerly wrote:


    1) For one reason or another, it occasionally happens that the "document.forms['com'].submit()" command suddenly stops working.


    2) The problem is, some search terms produce up to 500 results, and then AppleScript gets really(!) busy, and the fans in my mac spin like crazy. Once it gets through those entries, it returns to normal.


    1) Submit may not work before the page is fully loaded, so if (for some reason) the page loads slowly and the command gets sent too soon...  You could add in a repeat loop to check if the page is fully loaded, like so:


    tell application "Safari"

              repeat until ((do JavaScript "document.readyState" in document 1) = "complete")

      delay 1

              end repeat

    end tell


    but be aware that if the page doesn't load the script will get caught in an infinite loop and need to be quit using Command-..


    2) first, I suspect you can probably improve your search function. If you are searching through the entire list of done characters in a repeat loop, you're wasting tons of resources.  Do it this way instead:


    property doneTheseAlready : {}


    -- when you get a new character to test, do it this way

    if thisCharacter is in my doneTheseAlready then

      -- skip and move on to next


      -- save this character as completed

              copy thisCharacter to end of my doneTheseAlready

      -- do whatever downloading/processing you need to do

    end if


    That pushes the search off onto applescript itself, which is tremendously more efficient.  As a bonus, setting it as a property means you can quit the script and it will remember the list when you start up again.  Don't worry about it being Chinese characters; applescript handles unicode just fine.


    That being said, having your fans run high is not going to hurt anything - they are designed to do that.  Assuming you haven't blocked the vents, the fans are more than capable of keeping the machine within recommended operating temperatures.

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Oct 14, 2012 9:24 AM (in response to Queerly)

    1) well, the other option is that the page itself has some occasional irregularity that mucks up the DOM.  That couyld be jsut about anything (e.g. a miscode in PHP that sometimes drops or changes the name of the form element).  Unless I can see the html of the page on one of the times it doesn't work, your guess is as good as mine.


    2) yeah, it takes a while to learn the ins and outs of applescript.  it has intuitive tricks and shortcuts that you jsut have to learn.  If you want to post the full script (assuming it's not too long) I can help you optimize it, but I'm pretty sure the repeat loop you're using above is the source of your fan problem.


More Like This

  • Retrieving data ...

Bookmarked By (0)


  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.