Providing chromedriver path for new selenium version.

This commit is contained in:
Roland 2026-06-10 12:17:26 -06:00
parent d85c7721b5
commit 141a8b016e
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
3 changed files with 31 additions and 5 deletions

View file

@ -79,6 +79,9 @@ CHROMIUM = which('chromium-browser', 'chromium',
'/usr/bin/chromium-browser', # rpi os
'/usr/bin/chromium', # debian
)
CHROMEDRIVER_BIN = which('chromedriver',
'/usr/bin/chromedriver', # rpi os / debian
)
FIREFOX = which('firefox', 'firefox-esr',
'/usr/bin/firefox',
'/usr/bin/firefox-esr',

View file

@ -2544,17 +2544,23 @@ def format_html_file(file: pathlib.Path):
def html_screenshot(html: bytes | str) -> bytes:
"""Return a PNG screenshot of the provided HTML."""
from wrolpi import cmd # Avoid circular import.
# Set Chromium to headless. Use a wide window size so that screenshot will be the "desktop" version of the page.
options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('disable-gpu')
options.add_argument('window-size=1280x720')
# Use the system chromedriver; Selenium Manager downloads drivers from the internet, and has no binary for
# linux/aarch64 (Raspberry Pi).
service = webdriver.ChromeService(executable_path=str(cmd.CHROMEDRIVER_BIN)) if cmd.CHROMEDRIVER_BIN else None
with tempfile.NamedTemporaryFile('wb', suffix='.html') as fh:
fh.write(html.encode() if isinstance(html, str) else html)
fh.flush()
with webdriver.Chrome(options=options) as driver:
with webdriver.Chrome(options=options, service=service) as driver:
driver.get(f'file://{fh.name}')
driver.set_window_size(1280, 720)
screenshot = driver.get_screenshot_as_png()

View file

@ -894,15 +894,32 @@ def test_html_screenshot(singlefile_contents_factory):
def test_html_screenshot_webdriver_kwargs(singlefile_contents_factory):
"""Selenium 4.10 removed the `chrome_options` kwarg; `webdriver.Chrome` must be called with `options`."""
with mock.patch('wrolpi.common.webdriver.Chrome') as mock_chrome:
"""Selenium 4.10 removed the `chrome_options` kwarg; `webdriver.Chrome` must be called with `options`.
The system chromedriver must be passed explicitly because Selenium Manager downloads drivers from the
internet, and has no binary for linux/aarch64 (Raspberry Pi)."""
contents = singlefile_contents_factory()
with mock.patch('wrolpi.common.webdriver.Chrome') as mock_chrome, \
mock.patch('wrolpi.cmd.CHROMEDRIVER_BIN', pathlib.Path('/usr/bin/chromedriver')):
driver = mock_chrome.return_value.__enter__.return_value
driver.get_screenshot_as_png.return_value = b'PNG'
assert common.html_screenshot(singlefile_contents_factory()) == b'PNG'
assert common.html_screenshot(contents) == b'PNG'
assert mock_chrome.call_args.kwargs.keys() == {'options'}
assert mock_chrome.call_args.kwargs.keys() == {'options', 'service'}
assert isinstance(mock_chrome.call_args.kwargs['options'], common.webdriver.ChromeOptions)
assert mock_chrome.call_args.kwargs['service'].path == '/usr/bin/chromedriver'
# Without a system chromedriver (development machines), fall back to Selenium Manager.
with mock.patch('wrolpi.common.webdriver.Chrome') as mock_chrome, \
mock.patch('wrolpi.cmd.CHROMEDRIVER_BIN', None):
driver = mock_chrome.return_value.__enter__.return_value
driver.get_screenshot_as_png.return_value = b'PNG'
assert common.html_screenshot(contents) == b'PNG'
assert mock_chrome.call_args.kwargs['service'] is None
@pytest.mark.parametrize('color,expected', [