Copy the url address of the xPath

Is it possible to copy the address of the xPath in place of clicking it?

I am talking about the Chrome's click the xPath macro. It is excellent macro. I was thinking if copying the address is possible.

1 Like

That's a good idea for a KM feature request. @peternlewis

For now, you can use this simple JavaScript in Browser Script:

(function myMain () {
  'use strict';

var ptyScriptName   = "Get URL (href) Identified by XPath"
var ptyScriptVer     = "2.0"
var ptyScriptDate   = "2018-05-27"
var ptyScriptAuthor = "@JMichaelTX"
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PURPOSE:  Click on the Hyperlink Identified by the XPath provided by KM

RETURNS:  One of these, as text:
  • URL of xPath Element -- 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:
    //td[@class='category']//a

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) {
    returnResults = elemFound.href;
  }
  else {
    //--- ERROR:  ELEMENT WAS NOT FOUND ---
    returnResults = '[ERROR] Element NOT FOUND for XPath:\n' + xPathStr;
    alert(returnResults);
  }
  
  return returnResults;

})();

Here's an example Macro that uses this script:

MACRO:   Get Link Using XPath [Example]


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/3/b/3b83f6116dad0e6e943997d3b519599dafccf870.kmmacros">Get Link Using XPath [Example].kmmacros</a> (5.3 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---



![image|473x1149](upload://hMlJFMYejelFksVObcGSWkuDGIw.jpg)

---

Questions?
5 Likes

The xPath I find from inspecting Chrome was something like /html/body/table/tbody/tr/td[4]/a.

I don't know how to use that with your script.

Did you download my macro (which contains my script)?

All you need to do is enter the URL and Xpath in the indicated Actions:

image

1 Like

thank you, it worked.

1 Like

Hello!

@JMichaelTX

Is it possible to get xpath in case the browser is not frontmost (without bringing it frontmost) then extract URL from the xpath?

Or is there a way to get xpath from a webpage using JS?

I found a lot of examples on google but can't make any of them work for me

Yes.

The above macro/JavaScript should work fine even when the browser is NOT frontmost, as long as you only have one browser running.

If you have multiple browsers, or just want to target a specific browser, then instead of using the JavaScript in FrontBrowser, just use the action appropriate for the target browser.

  • JavaScript in Google Chrome
  • JavaScript in Safari

Questions?

Thank you, it works very well.

The only question is it possible to add / modify the action to execute JS in Google Chrome Canary instead of a regular GC as both of them are running.

Thanks.

That is not possible, AFAIK.

Hey Kirill,

You might be able to get Peter to add Google Chrome Canary to the Execute a JavaScript in Front Browser action, since it has essentially the same scripting dictionary as Google Chrome.

I think all he'd have to do is add its Bundle-ID (which is different than Chrome), although I'm not certain about that.

In any case you can easily script JavaScript in Canary.

-Chris

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/05/06 03:10
# dMod: 2018/11/27 14:09
# Appl: Google Chrome Canary
# Task: Do JavaScript in Google Chrome Canary Handler
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Google_Chrome_Canary, @JavaScript, @Handler
----------------------------------------------------------------

set jsCMD to "document.title"
set chromeDocTitle to doJavaScriptInChromeCanary(jsCMD)

----------------------------------------------------------------
--» HANDLERS
----------------------------------------------------------------
--» doJavaScriptInChromeCanary()
----------------------------------------------------------------
--  dMod: 2018/11/27 14:09
----------------------------------------------------------------
on doJavaScriptInChromeCanary(jsCMD)
   try
      tell application "Google Chrome Canary" to tell front window's active tab to execute javascript jsCMD
   on error e
      error "Error in handler doJavaScriptInChromeCanary() of library NLb!" & return & return & e
   end try
end doJavaScriptInChromeCanary
----------------------------------------------------------------
1 Like

Hey Chris,

Many thanks for the script, it does the work!
I couldn't figure it out how to run JS from AS but with your help i finally able to do it!
God bless you!

One more thing:
I am trying to run @JMichaelTX's javascript to execute URL from Xpath (taken from a km variable) in AS, after trying to run the JS from AS the result is "Missing value"

set jsCMD to "(function myMain () {
  'use strict';

var ptyScriptName   = \"Get URL (href) Identified by XPath\"
var ptyScriptVer     = \"2.0\"
var ptyScriptDate   = \"2018-05-27\"
var ptyScriptAuthor = \"@JMichaelTX\"
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PURPOSE:  Click on the Hyperlink Identified by the XPath provided by KM

RETURNS:  One of these, as text:
  • URL of xPath Element -- 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:
   //td[@class='category']//a

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_t_e2dp;
  
  var returnResults = \"TBD\";
  
  //--- GET THE HTML ELEMENT IDENTIFIED BY THE XPATH ---
  var elemFound =  document.evaluate(
      xPathStr, document, null, 0, null
    ).iterateNext();
    
  if (elemFound) {
    returnResults = elemFound.href;
  }
  else {
    //--- ERROR:  ELEMENT WAS NOT FOUND ---
    returnResults = '[ERROR] Element NOT FOUND for XPath:\\n' + xPathStr;
    alert(returnResults);
  }
  
  return returnResults;

})();"
set chromeDocTitle to doJavaScriptInChromeCanary(jsCMD)

----------------------------------------------------------------
--» HANDLERS
----------------------------------------------------------------
--» doJavaScriptInChromeCanary()
----------------------------------------------------------------
--  dMod: 2018/11/27 14:09
----------------------------------------------------------------
on doJavaScriptInChromeCanary(jsCMD)
	try
		tell application "Google Chrome Canary" to tell front window's active tab to execute javascript jsCMD
	on error e
		error "Error in handler doJavaScriptInChromeCanary() of library NLb!" & return & return & e
	end try
end doJavaScriptInChromeCanary

Any ideas how to fix it?

UPDATE:
I have found this script by @ccstone which solves the question.

set xpathStr to "//*[@id=\"grid\"]/table/tbody/tr[1]/td[17]/a"
set strJS to "

var xpathResults = document.evaluate('" & xpathStr & "', document, null, 0, null),
  nodeList = [],
  oNode;

while (oNode = xpathResults.iterateNext()) {
  nodeList.push(oNode.href);
}

nodeList;

"
tell application "Google Chrome Canary"
	
	tell application "Google Chrome Canary" to tell front window's active tab to execute javascript strJS
	
	
end tell

1 Like