Can you access XPaths in Firefox using AppleScript? ChatGPT thinks the answer is yes

Can you e.g, read a label or enter data into a form field using XPaths and AppleScript?

Hmm, ChatGPT came up with this answer:

Yes, it is possible to access XPaths in Firefox using AppleScript. Firefox provides a built-in accessibility API called Accessibility Services that allows AppleScript to interact with the browser’s user interface elements, including web pages and their elements.

Here’s an example AppleScript code that uses the Accessibility Services API to get the value of an element’s XPath in Firefox:

tell application "Firefox"
  activate
  tell application "System Events"
    tell process "Firefox"
      set theXPath to "xpath of the element to get"
      set elementValue to value of UI element of UI element 1 of (first UI element whose value of attribute "AXXPath" is theXPath)
    end tell
  end tell
end tell

In this code, you would replace "xpath of the element to get" with the XPath expression of the element you want to get the value of. The value property of the UI element object returns the value of the element, which you can then use as needed.

Note that Firefox must have accessibility enabled for this code to work. To enable accessibility in Firefox, go to Preferences > Privacy & Security > Permissions and check the box for “Allow accessibility services to access your browser”.

One part of this answer is wrong - there is no such preference as ChatGPT suggests. However, there is a setting in about:config accessibility.force_disabled that should be se to -1.

Anyway, when I run the code ChatGPT suggested I get an error message about the AXXPath attribute. Googling on that string doesn’t return anything useful. Is ChatGPT lost here or is it just a minor fix?

From your question, I guess that you’re referring to an HTML document currently visible in FF whose content you want to access via XPath and AppleScript. And that is, afaict, not possible. Scripting support in FF is very limited. You can get all tabs (windows) and their names. According to the scripting dictionary, every window should contain a document, but that is not accessible via AS.

The code you posted seems to (try to) access GUI components of FF. Which is, I’d think, not possible via XPath, since there’s no underlying XML document.

ChatGPT is just aping things it read somewhere (or invented, as in this case). I wouldn’t even trust it as far as I can throw a stone (which is about 20cm).

Alternative
Using Safari, it’s fairly simply to work with XPath (I’m using JavaScript here, AS should be similar)

const app = Application("Safari");
const scriptToRun = `const result = document.evaluate("XPath expression", null, 0, null);`
app.doJavaScript(scriptToRun);

This is, of course, not very useful: The result simply returned but not used. And anything you could use it for must happen in the browser context, i.e. in JavaScript code which you run via doJavaScript in Safari. You can’t pass DOM objects (which are the result of evaluating an XPath expression in the browser) back to your scripting code and do anything useful with them.

I think it is more easy to access ChatGPT API from AppleScript directly than via web browser.

http://piyocast.com/as/archives/15558

I think you misunderstood the OP: They were not asking how to access ChatGPT, but if what this piece of software told them was correct. Which in this case, as in many others, wasn’t.

1 Like

I think the way to avoid FireFox’s automation trouble is using other web browser or the other program.
FireFox is not suitable for automation.

You can use NSXMLDocument to load the HTML in AppleScript.

You can then use NSXMLNode and NSXMLElement methods to:
Run XPath queries on the Document (in XHTML format).

The get the values your looking for

As far as setting values ni

Your answer looks interrupted?

The code that ChatGPT showed you uses GUI scripting, like almost all tasks in Firefox, which is terribly unscriptable. Even to get the URL of the current page, you would have to use GUI scripting.

That is, in the solution @technomorph suggests, GUI scripting will still have to be used - to get the URL. Anyway, this will be a lot better then ChatGPT solution, as GUI scripting to get the URL is more or less standard, and the rest of the steps (using curl, NSXMLNode and NSXMLElement, running XPath queries on the Document (in XHTML format) doesn’t require GUI- scripting). For the 1st step (getting the URL) you can use following more or less standard code:

on firefoxCurrentTabURL()
	-- Store the current clipboard contents.
	set theClipboard to (the clipboard as record)
	-- Set the clipboard to a default blank value
	set the clipboard to ""
	-- Bring Firefox to the front, highlight the URL in the URL field and copy it.
	tell application "System Events"
		set frontmost of application process "firefox" to true
		keystroke "lc" using {command down}
	end tell
	-- Read the clipboard contents until either they change from "" or a second elapses.
	repeat 10 times
		delay 0.2
		set theURL to the clipboard
		if theURL is not "" then exit repeat
	end repeat
	-- Restore the old clipboard contents.
	set the clipboard to theClipboard
	return theURL
end firefoxCurrentTabURL

set theURL to firefoxCurrentTabURL()

Now about ChatGPT. Most likely, they gave you code that works on a specific Firefox version. This is one of the main problems with GUI scripting. If you have a different Firefox version, then the UI element of UI element 1 of (first UI element whose value of attribute “AXXPath” is theXPath) path will be different due to the different UI Elements structure.

You will have to determine the right path for yourself. The easiest way to do this is to use Automator. Turn on the record button (red) in Automator, go to Firefox, click on the desired element, stop recording, view the code written by Automator. In it you will find the full path to the clicked element you need.

As for my personal opinion, Firefox is clumsy for scripting, which means I agree with @Piyomaru: it’s better not to use it at all, and then all this complexity is not needed at all.

Yeah sorry, I’m on ios12.5 and the forum limits the amount of text I can see/type.
Pea one showed example of how to use the xpaths.

I recommend the app Xmlplify.

It allows you to import XML and a HTML via a IRL.
You can the find the elements you want and get the XPath at that point. It also has a XPath tool so you can test your xpaths

In peavines example you can also create a NSXMLDocument direct from a URL so you can skip the NSData part.

I would also recommend only creating the xmlDoc (NSXMLDocument) once.
Then run your XPaths on the one instance of it

But do that work with something like Facebook where you have a “feed” that adds more data as you scroll down?