XPath queries work in Safari and Chrome.
One way of getting the XPath to an element (for use in either browser) is to use Chrome's Copy As Xpath command:
Ctrl-click the clickable element of interest in Chrome
Choose Inspect Element
Select the relevant HTML element (typically an <a> element)
Ctrl-click again and choose Copy as XPath
Paste into the value field of the macro's Set Variable action
The .js code in the Execute JavaScript in Safari|Chrome actions is the same:
It:
Retrieves the kmvar.xPath variable from the browser document object
assumes that its value is a tested XPath expression, and passes it to the document.evaluate() function
clicks the first matching element on the front page in the browser
(function (strPath) {
(function (e) {
if (e.fireEvent) e.fireEvent('onclick');
else {
var o = document.createEvent('Events');
o.initEvent('click', true, false);
e.dispatchEvent(o);
}
})(
document.evaluate(
strPath, document, null, 0, null
).iterateNext()
);
})(document.kmvar.xPath);
Thanks @ComplexPoint, I worked with JMichaelTX on an xPath statement and couldn't get it to work. Reading through all the posts I tried your JavaScript and low and behold it works. Finally got one of the last three remaining "Statement Fetchers" to work.
Would anyone know if there Is a way to adapt this function to instead click on the FIRST xpath found, to be able to click on the nth occurrence of the xpath
I ask because a web page I am trying to automate uses the same xpath for all the buttons, so it would be ideal to be able to say "click on the 5th occurence of the xpath"
Thank you for the code, it works in most cases.
I have an issue with some element of the website.
For example I have this table and want to click on a element of this table (had to delete closing html tags as it wouldn't display correctly):
I'm just coming up to speed on web-automation / scraping. What are plusses / minuses of using XPath vs CSS selector vs DOM (getElementBy...). - Particularly for the environment of MacOS / Safari / AppleScript / JavaScript / KM?
I started with the last of those three, so its my comfort zone. For my use they seem to have a lot of overlap - I open a page in developer tools & can click a feature to find its 'magic string' - whether xpath, CSS selector, etc. And then use that magic string to reference it via script.
I haven't started using jquery yet, but i'll likely get there soon.
I have addressed this numerous times.
I find that querySelector() - Web APIs | MDN is much easier to use than XPath.
While you can get both the absolute path for Xpath and querySelector CSS using the Chrome tools, that path is rarely what you really want.
Often you will want a CSS that is either not dependent on its prior elements, or reoccurs throughout the web page.
Using querySelector takes only one statement to get the target HTML element, whereas Xpath requires multiple statements, often in a loop.
If you do a Forum search on "querySelector" you will find many of my posts on this subject.
There are also many posts on the Internet at Stackoverflow.com and elsewhere that provide good help on querySelector.
Looking for the same automation. My case is a little bit complicated:
first find a "div" element contains text "ABC" and click it.
then find a "span" element contains text "DEF" and click it.
finally find a "a" element that contains text "GHI" and click it.
Does the querySelector way works? Is the parameter for querySelector coming from chrome devtool "copy selector"?