axe-selenium-python

2.1.6 · maintenance · verified Thu Apr 16

axe-selenium-python is a Python library designed to integrate the axe-core accessibility testing engine with Selenium WebDriver for automated web accessibility testing. The original project (version 2.1.6) is largely in maintenance mode, with its last significant update in 2018. More actively maintained forks, such as `django-commons/axe-selenium-python` (version 3.0+), provide support for newer Python and Selenium versions and updated axe-core releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a Selenium WebDriver, inject the axe-core script using `Axe().inject()`, run accessibility checks with `Axe().run()`, and optionally save the results to a JSON file and report violations. It's crucial to have a compatible browser driver (like geckodriver or chromedriver) installed and accessible in your system's PATH.

import os
from selenium import webdriver
from axe_selenium_python import Axe

# Configure a browser (e.g., Firefox or Chrome)
# Ensure the appropriate WebDriver (e.g., geckodriver, chromedriver) is in your PATH
# For Chrome, you might need: driver = webdriver.Chrome()
driver = webdriver.Firefox()

try:
    driver.get("http://www.google.com")

    axe = Axe(driver)

    # Inject axe-core javascript into the page
    axe.inject()

    # Run axe accessibility checks
    results = axe.run()

    # Write results to a JSON file
    output_dir = "a11y_reports"
    os.makedirs(output_dir, exist_ok=True)
    axe.write_results(results, os.path.join(output_dir, 'google_a11y.json'))

    # Assert no violations are found (optional)
    if len(results["violations"]) > 0:
        print("Accessibility Violations Found:")
        print(axe.report(results["violations"]))
        # assert False, "Accessibility violations found!"
    else:
        print("No accessibility violations found.")

finally:
    driver.quit()

view raw JSON →