MACRO: [WEB] Click on Link Using XPath [Example]
~~~ VER: 2.0 2016-10-12 ~~~
DOWNLOAD:
[WEB] Click on Link Using XPath [Example].kmmacros (11 KB)
Video Demo
- See YouTube Video showing how to get XPath, and then demo of this macro.
See the below post for an example.
ReleaseNotes
Author.@JMichaelTX
PURPOSE:
- Click on the Hyperlink Identified by the XPath
- Requires either Safari or Google Chrome
MACRO SETUP
This method will work in both Safari and Chrome.
GET THE XPATH
We only need to use Chrome to get the XPath:
(Safari does not have the XPath tool)
- Open your web page in Chrome (just one time)
- Right-click on the link of interest, and select "Inspect"
- This will open the Chrome Developer tool, showing the HTML for the link
- Right-click on the HTML for the link, and select "Copy > Copy Xpath"
- We are now done with Chrome. You can close it.
- In the below Action, "Set KM xPath Variable",
- Paste in the text block -- this is the xpath. It should look someting like this:
//*[@id="ctl00_MainContent_ucEStatements_dlStatements_ctl00_lbStatementDate"]
POSSIBLE ISSUES WITH XPATH
- The "id" used may be dynamically created, and vary within the page, and in similar pages from the same web site. In this case, it may be not be reliable.
- If the HTML Element of interest does not have an "id" attribute, the XPath with be a relative path from the nearest element above that does have an "id".
- If the structure of the web page changes, this path could be broken, causing the XPath to fail.
REF:
USING THE MACRO:
- Make sure you have completed the above Setup
- Open your Browser to the page of interest
- Trigger this macro
TAGS: @XPath @Link @Chrome @Safari @JavaScript @Web @Browser
USER SETTINGS:
- Any Action in magenta color is designed to be changed by end-user
ACTION COLOR CODES
- To facilitate the reading, customizing, and maintenance of this macro,
key Actions are colored as follows: - GREEN -- Key Comments designed to highlight main sections of macro
- MAGENTA -- Actions designed to be customized by user
- YELLOW -- Primary Actions (usually the main purpose of the macro)
- ORANGE -- Actions that permanently destroy Varibles or Clipboards
REQUIRES:
(1) Keyboard Maestro Ver 7.2.1+
(2) Yosemite (10.10.5)+
(3) Either Safari or Google Chrome
JavaScript Used in Macro
(function run (pXPathStr) {
'use strict';
var ptyScriptName = "Click on Link Identified by XPath"
var ptyScriptVer = "2.0"
var ptyScriptDate = "2016-10-12"
var ptyScriptAuthor = "@JMichaelTX"
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PURPOSE: Click on the Hyperlink Identified by the XPath provided by KM
RETURNS: One of these, as text:
• "OK" -- if successful, no errors
• "[ERROR]" at start of results if a script error occurred.
KM VARIABALES REQUIRED:
• xPath -- the full XPath to the Link
EXAMPLE XPATH:
//*[@id="ctl00_MainContent_ucEStatements_dlStatements_ctl00_lbStatementDate"]
KM VARIABLES SET:
• NONE
REF:
• [Introduction to using XPath in JavaScript](https://developer.mozilla.org/en-US/docs/Introduction_to_using_XPath_in_JavaScript)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
//--- GET THE XPATH FROM KM ---
var xPathStr = document.kmvar.xPath;
var returnResults = "TBD";
//--- GET THE HTML ELEMENT IDENTIFIED BY THE XPATH ---
var elemFound = document.evaluate(
xPathStr, document, null, 0, null
).iterateNext();
if (elemFound) {
//--- CLICK ON THE LINK ---
elemFound.click();
returnResults = "OK";
}
else {
//--- ERROR: ELEMENT WAS NOT FOUND ---
returnResults = '[ERROR] Element NOT FOUND for XPath:\n' + xPathStr;
alert(returnResults);
}
return returnResults;
})();