Basic example using the python axe-devtools-api

Link to Basic example using the python axe-devtools-api copied to clipboard

The following is an minimal example of testing the page using axe-devtools-api and axe-devtools-selenium.

Prerequisites

  • Python 3
  • Selenium webdriver binding (i.e. chromedriver or geckodriver)
  • Install axe-devtools-api and axe-devtools-selenium via either Agora or bundles

Code

This opens a page with intentional accessibility violations in a Chrome window, runs axe, then pretty prints the results.

from pprint import pprint
from selenium import webdriver
from axe_devtools_selenium import AxeDriver
from axe_devtools_api import Axe

page = webdriver.Chrome()
page.get("https://dequeuniversity.com/demo/mars/")

axe = Axe(AxeDriver(page))

results = axe.analyze()
pprint(results.__dict__)

This does the same, except that it throws an exception if there are a11y violations instead of printlng results

from pprint import pprint
from selenium import webdriver
from axe_devtools_selenium import AxeDriver
from axe_devtools_api import Axe

page = webdriver.Chrome()
page.get("https://dequeuniversity.com/demo/mars/")

axe = Axe(AxeDriver(page))

results = axe.analyze()
assert results.is_axe_clean(), results.violations_report()

If you want to do something to the page before scanning, please consult the selenium docs or the api docs.

If you want to use Firefox then just change webdriver.Chrome() to webdriver.Firefox().