From f7f3d730ee33f0064828fb1986a283e8c30a2498 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Thu, 10 Mar 2022 14:27:59 -0800 Subject: [PATCH] shot-scraper accessibility command, closes #22 --- .github/workflows/test.yml | 1 + README.md | 6 ++++++ shot_scraper/cli.py | 20 ++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d64d58b..50ae89a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -46,6 +46,7 @@ jobs: --selector 'section.secondary' \ -o examples/owlsnearme-wait.jpg \ --wait 2000 + shot-scraper accessibility https://datasette.io/ > examples/datasette-accessibility.json # And using multi echo ' - output: examples/example.com.png diff --git a/README.md b/README.md index daf8aee..a2c128e 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,12 @@ You can include desired `height`, `width`, `quality` and `wait` options on each wait: 500 ``` +## Dumping out an accessibility tree + +The `shot-scraper accessibility` command dumps out the Chromium accessibility tree for the provided URL, as JSON: + + shot-scraper accessibility https://datasette.io/ + ## Development To contribute to this tool, first checkout the code. Then create a new virtual environment: diff --git a/shot_scraper/cli.py b/shot_scraper/cli.py index af1f073..b1f3636 100644 --- a/shot_scraper/cli.py +++ b/shot_scraper/cli.py @@ -1,5 +1,6 @@ import click from click_default_group import DefaultGroup +import json from playwright.sync_api import sync_playwright from runpy import run_module import sys @@ -106,6 +107,25 @@ def multi(config): browser.close() +@cli.command() +@click.argument("url") +def accessibility(url): + """ + Dump the Chromium accessibility tree for the specifed page + + Usage: + + shot-scraper accessibility https://datasette.io/ + """ + with sync_playwright() as p: + browser = p.chromium.launch() + page = browser.new_page() + page.goto(url) + snapshot = page.accessibility.snapshot() + browser.close() + click.echo(json.dumps(snapshot, indent=4)) + + @cli.command() def install(): """