AI-powered element locator for Selenium WebDriver
Project description
LocatAI
AI-powered element locator for Selenium WebDriver.
Overview
LocatAI is an innovative Python package that uses AI to find web elements based on natural language descriptions, making your Selenium tests more reliable and easier to maintain. Instead of brittle CSS selectors or XPath expressions, you can use natural language to describe the elements you want to interact with.
Features
- Natural Language Element Finding: Locate elements using plain English descriptions
- Multiple AI Providers: Support for OpenAI GPT-4 and Anthropic Claude
- Smart Caching: Reduces API calls and improves performance
- Automatic Timeout Management: Dynamically adjusts timeouts based on historical performance
- Detailed Error Diagnostics: Helps debug element location failures
- Usage Analytics: Tracks API usage, cache efficiency, and success rates
Installation
Install the package using pip:
pip install locatai
Environment Setup
LocatAI supports both OpenAI and Anthropic Claude for element location. You need to set up your API keys and configure which provider and model to use:
- Create a
.envfile in your project's root directory - Add your configuration to the file:
# API Keys
OPENAI_API_KEY=your_openai_api_key_here
ANTHROPIC_API_KEY=your_anthropic_api_key_here
# Provider Configuration
# Uncomment the provider you want to use (default is OpenAI)
AI_PROVIDER=openai
# AI_PROVIDER=anthropic
# Model Configuration
# OpenAI Models (default: gpt-4)
OPENAI_MODEL=gpt-4
# Other options: gpt-4-turbo-preview, gpt-3.5-turbo, etc.
# Anthropic Models (default: claude-3-opus-20240229)
ANTHROPIC_MODEL=claude-3-opus-20240229
# Other options: claude-3-sonnet-20240229, claude-3-haiku-20240307, etc.
- Important: Add the
.envfile to your.gitignoreto prevent accidentally committing your API keys
The package uses the python-dotenv library to automatically load these settings when needed.
Basic Usage
from selenium import webdriver
from locatai import ElementFinder
# Initialize WebDriver
driver = webdriver.Chrome()
driver.get("https://example.com")
# Find an element using natural language
login_button = ElementFinder.FindElementByAI(driver, "Login button")
login_button.click()
# Find multiple elements
item_cards = ElementFinder.FindElementsByAI(driver, "product cards")
print(f"Found {len(item_cards)} products")
Advanced Usage
Choosing an AI Provider
LocatAI supports two AI providers:
- OpenAI (GPT-4) - Default provider
- Anthropic (Claude) - Alternative provider with different capabilities
You can switch providers by setting the AI_PROVIDER environment variable in your .env file:
# Use OpenAI
AI_PROVIDER=openai
# Use Anthropic Claude
AI_PROVIDER=anthropic
Smart Timeouts
LocatAI automatically adjusts wait times based on historical performance:
# Default timeout will be used (smart timeout based on element history)
submit_button = ElementFinder.FindElementByAI(driver, "Submit button")
# Override with a custom timeout in seconds
menu_item = ElementFinder.FindElementByAI(driver, "Settings menu item", timeout=15)
Tracking API Usage
Monitor your AI API usage and performance metrics:
from locatai import AIUsageTracker
# Get usage statistics
report = AIUsageTracker.get_instance().get_report()
print(f"API Calls: {report['api_calls']}")
print(f"Cache Hit Rate: {report['cache_hit_rate']:.1%}")
print(f"Estimated Cost: ${report['estimated_cost']:.4f}")
See the examples directory for more advanced usage patterns.
How It Works
LocatAI analyzes the current DOM of your web page and uses AI (either OpenAI's GPT-4 or Anthropic's Claude) to determine the most reliable selector strategy for your element description. It prioritizes the most stable selectors (ID, name) when available, and falls back to CSS selectors or XPath when needed.
The package includes:
- Smart caching to reduce API calls for previously seen elements
- Automatic retries with different locator strategies
- Detailed error diagnostics when elements can't be found
- Performance tracking to optimize timeouts
Troubleshooting
API Key Issues
- Ensure your
.envfile is in the correct location (project root directory) - Verify your API keys are valid for your chosen provider
- Check that the environment variable names are exactly
OPENAI_API_KEYand/orANTHROPIC_API_KEY - Verify the
AI_PROVIDERsetting matches your intended provider
Element Not Found Issues
- Try using more specific element descriptions
- Ensure the element is visible in the DOM when the finder method is called
- Check if the element is inside an iframe (switch to it first)
- Increase the timeout for elements that take longer to load
- Try switching AI providers as they may have different capabilities
Contributing
Contributions are welcome! Please feel free to submit a Pull Request to https://github.com/Divyarajsinh-Dodia/locatai.
License
MIT
Project details
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 locatai-0.1.8.tar.gz.
File metadata
- Download URL: locatai-0.1.8.tar.gz
- Upload date:
- Size: 19.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
93bb79e7af8e3c2dfb13f700a23beb51a8e9cd53b3da35d0c3d5fe628f8c77ca
|
|
| MD5 |
1725cfee4552e44a0519acf1ab846e24
|
|
| BLAKE2b-256 |
ed194e2a331e6f0fa26e5139f5dacc97b6901925a4c53b22bf576478815bd5d6
|
File details
Details for the file locatai-0.1.8-py3-none-any.whl.
File metadata
- Download URL: locatai-0.1.8-py3-none-any.whl
- Upload date:
- Size: 22.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b6952beb0f7fac354cf53c14fb33599281624769f83d6ffa94de558a48219dc
|
|
| MD5 |
5c03ba649b7da455ffd70df1d95f17a2
|
|
| BLAKE2b-256 |
7c334a76d2a921105f0e3691ceaa1fac1e99d522f59ac0b581e184d506ac34cb
|