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.

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.