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"))
)
AI Service Setup
This package requires a local AI service with an OpenAI-compatible API. We recommend using VS Code Copilot as Service, which exposes GitHub Copilot through a local HTTP server.
Installing VS Code Copilot as Service
-
Install from VS Code Marketplace or run:
code --install-extension MartyZhou.vscode-copilot-as-service
-
The extension automatically starts a server on
http://localhost:8765 -
Requires an active GitHub Copilot subscription
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 (
install_auto_correct_hook,
get_auto_correct,
get_correction_tracker,
export_corrections_report
)
install_auto_correct_hook()
auto_correct = get_auto_correct()
auto_correct.enabled = True
auto_correct.suggest_better_selectors = False
# Export corrections report at end of test run
tracker = get_correction_tracker()
export_corrections_report("corrections_report.json")
tracker = get_correction_tracker()
export_corrections_report("corrections_report.json")
print(f"Total corrections: {len(tracker.get_corrections())}")
print(f"Successful corrections: {len(tracker.get_successful_corrections())}")
Custom AI Provider
from selenium_selector_autocorrect import AIProvider, configure_provider
class CustomAIProvider(AIProvider):
def is_available(self) -> bool:
return True
def suggest_selector(self, system_prompt: str, user_prompt: str):))
How It Works
- Hook Installation: Patches
WebDriverWait.until()to add auto-correction - Timeout Detection: When a selector times out, the original exception is caught
- Page Analysis: JavaScript extracts visible elements and their attributes
- AI Suggestion: Sends page context to AI provider for selector suggestion
- Verification: Tests the suggested selector
- Success Handling: If successful, records the correction and optionally updates the test file
- Fallback: If correction fails, raises the original TimeoutException
AI Provider Setup
Local AI Service
The package requires a local AI service with OpenAI-compatible API:
POST http://localhost:8765/v1/chat/completions
For file auto-updates:
POST http://localhost:8765/v1/workspace/files/read
POST http://localhost:8765/v1/workspace/files/edit
## Correction Reports
Export correction reports in JSON format:
```python
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"
}
}
Best Practices
- Install Once: Call
install_auto_correct_hook()once at test suite startup (e.g., inconftest.py) - Review Corrections: Regularly review correction reports to identify brittle selectors
- Update Tests: Use auto-update sparingly and review changes before committing
- Monitor AI Service: Ensure your AI service is running and responsive
- Use Strong Selectors: The tool helps with failures but writing robust selectors is still preferred
Requirements
- Python >= 3.8
- selenium >= 4.0.0
- requests >= 2.25.0
License
MITInstall hook once at test suite startup (e.g., in conftest.py) 2. Review correction reports regularly to identify brittle selectors 3. Use auto-update sparingly and review changes before committing 4. Ensure your AI service is running and responsive 5. Write robust selectors - the tool helps with failures but prevention is better
When contributing:
- Follow PEP 8 style guidelines
- Add tests for new features
- Update documentation
- No emojis in code or documentation
Troubleshooting
AI Service Not Available
Contributions are welcome! Please:
- Follow PEP 8 style guidelines
- Add tests for new features
- Update documentation
- Maintain consistency with existing code
Possible causes:
SELENIUM_AUTO_UPDATE_TESTSnot set to"1"- Test file path not detected correctly
- Selector string not found in source file (check quotes)
No Corrections Happening
Solution: Ensure your local AI service is running on the configured port.
Test File Not Updated
Possible causes:
SELENIUM_AUTO_UPDATE_TESTSnot set to "1"- Test file path not detected correctly
- Selector string not found in source file
No Corrections Happening
Check:
- Hook is installed - look for log message
- AI service is available - check
get_auto_correct().is_service_available() - Auto-correct is enabled - c See CHANGELOG.md for version history and changes.
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.0.tar.gz.
File metadata
- Download URL: selenium_selector_autocorrect-0.1.0.tar.gz
- Upload date:
- Size: 16.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5655d60048ab6ca091fae3661b8397de91740c390ef7a2cdaa0788e53169e875
|
|
| MD5 |
acc22315d19b3633f8637474fea02912
|
|
| BLAKE2b-256 |
98858fb66aab1b33f806bd9eb0a4d8fef3e9b63bd74d92143e04b1b5d5af06a9
|
File details
Details for the file selenium_selector_autocorrect-0.1.0-py3-none-any.whl.
File metadata
- Download URL: selenium_selector_autocorrect-0.1.0-py3-none-any.whl
- Upload date:
- Size: 15.2 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 |
9469f0fe88bc24a5acf94d2486c0485fd04685bf5a8c046cae5c39f946f3e5e2
|
|
| MD5 |
57229e0f7ab31e446c4e1f7a06813306
|
|
| BLAKE2b-256 |
d1d623ff2a6bfaf9a6268bfcab261609cbcd087febc3810fa2bd8e49c7ee36f9
|