Skip to main content

Get HTML elements locators using natural language.

Project description

Locatr

Go Reference Test

Locatr package helps you to find HTML locators on a webpage using prompts and llms.

Overview

  • LLM based HTML element css path finder.
  • Re-rank support for improved accuracy.
  • Supports playwright, selenium, cdp.
  • Uses cache to reduce calls to llm apis.
  • Results/Statistics generation of api calls.

Example:

print(locatr.get_locatr("Search input bar in the page"))
# output: 'html > div > input'

For more examples check the examples/python folder.

Install locatr with

pip install locatr

Table of Contents

Quick Example

# example assumes that there is already a page opened in the selenium session.
import os

from locatr import (
    LlmProvider,
    LlmSettings,
    Locatr,
    LocatrCdpSettings,
    LocatrSeleniumSettings,
    PluginType,
)

llm_settings = LlmSettings(
    llm_provider=LlmProvider.OPENAI,
    llm_api_key=os.environ.get("LLM_API_KEY"),
    model_name=os.environ.get("LLM_MODEL_NAME"),
    reranker_api_key=os.environ.get("RERANKER_API_KEY"),
)

locatr_settings_selenium = LocatrSeleniumSettings(
    plugin_type=PluginType.SELENIUM,
    llm_settings=llm_settings,
    selenium_url=os.environ.get("SELENIUM_URL"), # url must end with `/wd/hub`
    selenium_session_id="e4c543363b9000a66073db7a39152719",
)

selenium_locatr = Locatr(locatr_settings_selenium, debug=True)

print(selenium_locatr.get_locatr("H1 element with text Example Domain"))

Locatr Settings

Locatr settings expects the following fields:

Cache Settings.

  • cache_path -> The path to the cache file. This file will be reused later to save llm api requests. If nothing is provided then it is stored in .locatr.cache.
  • use_cache -> Weather to use cache or not.

For cache schema see: link

Llm settings.

Contains all the settings required for llm and re-ranking.

from locatr import LlmSettings

It expects the following values:

  1. llm_provider -> The llm provider you want to use. Options are locatr.LlmProvider.OPENAI and locatr.LlmProvider.ANTHROPIC
  2. llm_api_key -> The provider's api key value.
  3. model_name -> Specify which llm model you want to use.
  4. reranker_api_key -> Api key for cohere reranker. It is optional if not provided reranking will not be used.

Example:

from locatr import LlmSettings

llm_settings = LlmSettings(
    llm_provider=LlmProvider.OPENAI,
    llm_api_key=os.environ.get("LLM_API_KEY"),
    model_name=os.environ.get("LLM_MODEL_NAME"),
    reranker_api_key=os.environ.get("RERANKER_API_KEY"),
)

Note: Except for reranker_api_key if values are not provided in the settings then they will be read from the following env variables.

  • LLM_PROVIDER
  • LLM_MODEL
  • LLM_API_KEY

Locatr settings is bound with the type of plugin you want to use (cdp/selenium).

To create settings for cdp use (use with playwright):

from locatr import LocatrCdpSettings

# .... create llm settings

locatr_setting_cdp = LocatrCdpSettings(
    llm_settings=llm_settings,
    cdp_url="http://localhost:9222", # You can get this port by passing the following argument to chromium based browsers: `--remote-debugging-port=9222`
)

To create settings for selenium use:

from locatr import LocatrSeleniumSettings

locatr_settings_selenium = LocatrSeleniumSettings(
    llm_settings=llm_settings,
    selenium_url=os.environ.get("SELENIUM_URL"), # url must end with `/wd/hub`
    selenium_session_id="e4c543363b9000a66073db7a39152719",
)

Get locatr

To get locatr string we need to import the Locatr class and pass the settings to it.

from locatr import Locatr

# ... create settings 

l = Locatr(locatr_settings_selenium)

By default, the Locatr class operates without logging. However, if you'd like to view the Locatr server logs for debugging purposes, you can enable the debug parameter by passing True as the second argument during initialization.

l.get_locatr("red 'yes button' in the form")

You can also get locatrs asynchronously. Just call get_locatr_async.

    await l.get_locatr_async("red 'yes button' in the form")

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

test_locatr-0.22.0.tar.gz (5.9 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

test_locatr-0.22.0-py3-none-any.whl (5.9 MB view details)

Uploaded Python 3

File details

Details for the file test_locatr-0.22.0.tar.gz.

File metadata

  • Download URL: test_locatr-0.22.0.tar.gz
  • Upload date:
  • Size: 5.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for test_locatr-0.22.0.tar.gz
Algorithm Hash digest
SHA256 d1a29a10aeaa6255f0f5a095912474bd0f79475369a8edea3d7702e87ccf7426
MD5 cc67f3b4875e0b7c6eb3761ade801c4b
BLAKE2b-256 d468b321b8bc9901b1ad2a90b7430b5089b21395ecd7b24451989d10adce202c

See more details on using hashes here.

File details

Details for the file test_locatr-0.22.0-py3-none-any.whl.

File metadata

  • Download URL: test_locatr-0.22.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for test_locatr-0.22.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fa110de3162df9ff661af8c6a8e69bf21720ccc97cfc214516be9a9088a30cd8
MD5 17d52eb1df12d7c2f2a63e6c0f36dd79
BLAKE2b-256 861213e6fa9812ac10d58525cdca2dc255c5a24b80d549efb9a0b09d8d2b2124

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page