No internet connection
  1. Home
  2. How to

How to click on button or link in a website.

By Andrew Sherman @Andrew_Sherman
    2021-01-28 20:22:55.954Z

    Is it possible to select something specific on a web page? Previously using Keyboard Maestro I've done something like:

    • open specific URL
    • Wait until finished loading
    • Click on link "/html/body/div[6]/div/form/div[1]/div[5]/div/button"

    I'd like to avoid "click relative to UI element" as it doesn't seem the most robust method for this

    • 5 replies
    1. Hi Andrew,

      Which browser are you using?

      1. AAndrew Sherman @Andrew_Sherman
          2021-01-29 07:54:22.259Z

          Normally Chrome, but it would be great if the functionality supported the default browser

          1. We don't have a cross browser implementation for this (although I agree it would be great).

            Here's a script that will enter SoundFlow into the search field of the google front page (www.google.com) and hit Search.

            function executeJs(script) {
                var res = sf.system.execAppleScript({
                    script: 'tell application "Google Chrome"\n' +
                    'set str to execute in active tab of the front window JavaScript "' + script.replace(/"/g, '\\"') + '"\n' +
                    'str\n' +
                    'end tell\n'
                });
                if (res.standardError) {
                    log(res.standardError);
                    throw 0;
                }
                return res.result;
            }
            
            function searchGoogleInOpenTab(query) {
                executeJs(`document.querySelector('input[role=combobox]').value = ${JSON.stringify(query)}`);
                executeJs(`document.querySelector('input[type=submit]').click()`);
            }
            
            searchGoogleInOpenTab('SoundFlow');
            
            1. AAndrew Sherman @Andrew_Sherman
                2021-01-29 13:15:10.500Z

                Thanks, I'll use that as a starting point.

                1. Here's one that can also be used with Safari:

                  
                  function executeJs(appName, script) {
                      var res;
                      if (appName === 'chrome') {
                          res = sf.system.execAppleScript({
                              script: `tell application "Google Chrome"
                          set str to execute in active tab of the front window JavaScript "${script.replace(/"/g, '\\"')}"
                          str
                          end tell`
                          });
                      } else if (appName === 'safari') {
                          res = sf.system.execAppleScript({
                              script: `tell application "Safari"
                          set str to do JavaScript "${script.replace(/"/g, '\\"')}" in document 1
                          str
                          end tell`
                          });
                      } else {
                          throw `Unsupported app: ${appName}`;
                      }
                      if (res.standardError) {
                          log(res.standardError);
                          throw 0;
                      }
                      return res.result;
                  }
                  
                  function searchGoogleInOpenTab(query) {
                      var appName = 'safari'; //or 'chrome'
                      executeJs(appName, `document.querySelector('input[role=combobox]').value = ${JSON.stringify(query)}`);
                      executeJs(appName, `document.querySelector('input[type=submit]').click()`);
                  }
                  
                  searchGoogleInOpenTab('SoundFlow');
                  

                  Please note though, that Safari has more security restrictions, so the user has to do some manual things to allow remote control.