Automatic Selenium selector correction using AI when elements fail to be found
Project description
Selenium Selector AutoCorrect
A Python package that automatically corrects Selenium element selectors using AI when they fail, reducing test maintenance and improving test reliability.
Features
- Automatic Selector Correction: When a WebDriverWait times out, the package uses AI to analyze the page and suggest working alternatives
- Local AI Integration: Uses a local AI service with OpenAI-compatible API
- Correction Tracking: Records all corrections with source file and line information
- Optional Auto-Update: Can automatically update test files with corrected selectors
- Zero Code Changes: Works by hooking into Selenium's WebDriverWait
Installation
pip install selenium-selector-autocorrect
Quick Start
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium_selector_autocorrect import install_auto_correct_hook
install_auto_correct_hook()
driver = webdriver.Chrome()
driver.get("https://example.com")
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "some-element"))
)
Configuration
Configure via environment variables:
LOCAL_AI_API_URL: URL of local AI service (default:http://localhost:8765)SELENIUM_AUTO_CORRECT: Enable/disable auto-correction (default:"1")SELENIUM_SUGGEST_BETTER: Suggest better selectors for found elements (default:"0")SELENIUM_AUTO_UPDATE_TESTS: Auto-update test files with corrections (default:"0")
Example
import os
os.environ['LOCAL_AI_API_URL'] = 'http://localhost:8765'
os.environ['SELENIUM_AUTO_CORRECT'] = '1'
os.environ['SELENIUM_AUTO_UPDATE_TESTS'] = '1' # Enable auto-update
Usage
Basic Usage
from selenium_selector_autocorrect import install_auto_correct_hook
install_auto_correct_hook()
Advanced Usage
from selenium_selector_autocorrect import (
get_auto_correct,
get_correction_tracker,
export_corrections_report,
)
auto_correct = get_auto_correct()
auto_correct.enabled = True
tracker = get_correction_tracker()
export_corrections_report("corrections_report.json")
AI Service Setup
This package requires a local AI service with an OpenAI-compatible API. The following endpoints are used:
POST {LOCAL_AI_API_URL}/v1/chat/completions— chat completions for suggestionsPOST {LOCAL_AI_API_URL}/v1/workspace/files/read— read file contentPOST {LOCAL_AI_API_URL}/v1/workspace/files/edit— apply edits to filesPOST {LOCAL_AI_API_URL}/v1/workspace/files/search— search workspace
Exporting Reports
from selenium_selector_autocorrect import export_corrections_report
export_corrections_report("corrections_report.json")
Report format:
{
"corrections": [
{
"original_by": "id",
"original_value": "old-selector",
"corrected_by": "css selector",
"corrected_value": ".new-selector",
"success": true,
"test_file": "/path/to/test.py",
"test_line": 42,
"timestamp": "2024-01-31T10:30:00"
}
],
"summary": {
"total": 10,
"successful": 8,
"generated_at": "2024-01-31T10:35:00"
}
}
Troubleshooting
AI service not available: Ensure the local AI service is running and reachable via LOCAL_AI_API_URL.
Auto-update not running: Verify SELENIUM_AUTO_UPDATE_TESTS is set to "1".
Selector strings not found when updating: Check quote styles in your source files match those used in the correction.
Requirements
- Python >= 3.8
- selenium >= 4.0.0
- requests >= 2.25.0
License
MIT
Contributing
Please follow PEP 8, add tests for new features, and update documentation when changing behavior.
See CHANGELOG.md for release notes and version history.
Publishing to PyPI
Create a PyPI API token at https://pypi.org/manage/account/#api-tokens (recommended scope: project or account) and keep the token secret. PyPI no longer accepts username/password uploads — use the token as the password and __token__ as the username.
PowerShell (Windows) example:
$env:TWINE_USERNAME='__token__'
$env:TWINE_PASSWORD='pypi-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
.\venv\Scripts\python.exe -m pip install --upgrade build twine
.\venv\Scripts\python.exe -m build
.\venv\Scripts\python.exe -m twine upload dist/*
Unix / macOS example:
export TWINE_USERNAME='__token__'
export TWINE_PASSWORD='pypi-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
python3 -m pip install --upgrade build twine
python3 -m build
python3 -m twine upload dist/*
To publish to TestPyPI (verify release first):
python3 -m twine upload --repository-url https://test.pypi.org/legacy/ dist/*
Notes:
- Use an API token (not account password). If using a CI system, store the token in secure secrets.
- You can add a persistent
~/.pypircfor repeated uploads; see PyPI documentation for details. - If upload fails with a 403, verify the token is correct and has the required scope.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file selenium_selector_autocorrect-0.1.2.tar.gz.
File metadata
- Download URL: selenium_selector_autocorrect-0.1.2.tar.gz
- Upload date:
- Size: 21.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d22df50afa0af12aa00139f924fca0299cf7901071ca282293ac1d2564db3fb4
|
|
| MD5 |
191bf48c8c9722fa88bff5f77fcccb56
|
|
| BLAKE2b-256 |
f8647cfc7c915a4a24d980df6473df0d3d37c94126c8a1aec87acb16a98ca69a
|
File details
Details for the file selenium_selector_autocorrect-0.1.2-py3-none-any.whl.
File metadata
- Download URL: selenium_selector_autocorrect-0.1.2-py3-none-any.whl
- Upload date:
- Size: 20.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f304000e778e644948a512fe1eac1f45a52a2ec6b0e0a4fa323ad0584696b70
|
|
| MD5 |
b570207ba2c9b3078e4fa5ba4971f8cc
|
|
| BLAKE2b-256 |
813e8bc36315dec8ed17fdbc81a9021508525f651581b0b915fd494995717ab7
|