Skip to content

Commit

Permalink
feat: add more python examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Neeraj319 committed Dec 26, 2024
1 parent 3cf6adb commit 592d1b2
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
27 changes: 27 additions & 0 deletions examples/python/example_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import asyncio
import os

from locatr import LlmProvider, LlmSettings, Locatr, LocatrCdpSettings

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

# there is already a page opened for url: https://example.com/
locatr_settings_cdp = LocatrCdpSettings(
llm_settings=llm_settings,
cdp_url="http://localhost:9222",
)


async def main():
selenium_locatr = Locatr(locatr_settings_cdp)
print(
await selenium_locatr.get_locatr_async("Link to 'More information...'")
)


asyncio.run(main())
44 changes: 44 additions & 0 deletions examples/python/playwright.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os
import time

from playwright.sync_api import sync_playwright

from locatr import LlmProvider, LlmSettings, Locatr, LocatrCdpSettings


def setup_locatr() -> Locatr:
llm_settings = LlmSettings(
llm_provider=LlmProvider.OPENAI,
llm_api_key=os.environ.get("LLM_API_KEY"),
model_name=os.environ.get("LLM_MODEL"),
reranker_api_key=os.environ.get("COHERE_API_KEY"),
)
locatr_settings_cdp = LocatrCdpSettings(
llm_settings=llm_settings,
cdp_url="http://localhost:9222",
)
return Locatr(locatr_settings_cdp)


def main(cdp_locatr: Locatr):
with sync_playwright() as p:
browser = p.chromium.launch(
headless=False, args=["--remote-debugging-port=9222"]
)
page = browser.new_page()
page.goto("https://store.steampowered.com/")
time.sleep(5)

search_bar_selector = cdp_locatr.get_locatr(
"Search input bar on the steam store."
)

search_barch_locatr = page.locator(search_bar_selector)
search_barch_locatr.first.fill("Counter Strike 2")
search_barch_locatr.first.press("Enter")

time.sleep(5)


if __name__ == "__main__":
main(setup_locatr())
48 changes: 48 additions & 0 deletions examples/python/selenium.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

from locatr import LlmProvider, LlmSettings, Locatr, LocatrSeleniumSettings


def setup_locatr(selenium_url: str, selenium_session_id: str) -> Locatr:
llm_settings = LlmSettings(
llm_provider=LlmProvider.OPENAI,
llm_api_key=os.environ.get("LLM_API_KEY"),
model_name=os.environ.get("LLM_MODEL"),
reranker_api_key=os.environ.get("COHERE_API_KEY"),
)
locatr_settings_cdp = LocatrSeleniumSettings(
llm_settings=llm_settings,
selenium_url=selenium_url,
selenium_session_id=selenium_session_id,
)
return Locatr(locatr_settings_cdp)


def main():
opts = Options()
dr = webdriver.Remote(
command_executor="http://127.0.0.1:4444/wd/hub", options=opts
)
try:
dr.get("https://hub.docker.com/")
time.sleep(5)
locatr = setup_locatr(
"http://127.0.0.1:4444/wd/hub", str(dr.session_id)
)
search_bar_path = locatr.get_locatr("Search Docker Hub input field")
element = dr.find_element(By.CSS_SELECTOR, search_bar_path)
element.send_keys("Python")
element.send_keys("Enter")
time.sleep(2)
print(dr.title)
finally:
dr.quit()


if __name__ == "__main__":
main()

0 comments on commit 592d1b2

Please sign in to comment.