diff --git a/examples/python/example_async.py b/examples/python/example_async.py new file mode 100644 index 0000000..f42dcf6 --- /dev/null +++ b/examples/python/example_async.py @@ -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()) diff --git a/examples/python/playwright.py b/examples/python/playwright.py new file mode 100644 index 0000000..cd433d1 --- /dev/null +++ b/examples/python/playwright.py @@ -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()) diff --git a/examples/python/selenium.py b/examples/python/selenium.py new file mode 100644 index 0000000..45d185b --- /dev/null +++ b/examples/python/selenium.py @@ -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()