A quick way to run axe-core from python

matmooredev

Mat

Posted on February 13, 2024

A quick way to run axe-core from python

I used this code snippet today to run axe-core from a python selenium test. It calls out to the command line interface, using npx so I don't have to worry about managing node packages:

    def check_for_accessibility_issues(self):
        command = ["npx", "@axe-core/cli", "-q", self.selenium.current_url]
        output = subprocess.run(command, capture_output=True, text=True)
        self.assertEqual(output.returncode, 0, output.stdout)
Enter fullscreen mode Exit fullscreen mode

This should work with anything that runs your app in a browser, not just selenium.

Axe is an automated tool to detect some (but not all) web accessibility issues. It's not a substitute for actual usability testing with real people, but it's still useful. For a rough idea of what it can do, check out this blog post: Axe-Core vs PA11y. In the author's testing, axe-core detected 27% of known issues on a test page.

I was originally hoping to use axe-selenium-python for this. It uses selenium's execute_script to inject the axe javascript into the page, similar to this method. Unfortunately, axe-selenium-python has been unmaintained since its author left mozilla. It looks like some nice people are going to take this over, but I wanted a workaround in the meantime.

Note: There is also Deque DevTools, which appears to have python bindings, but this is not open source. 😞

💖 💪 🙅 🚩
matmooredev
Mat

Posted on February 13, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related