From b3f6b8b5214593ba2716ef4a527c621c9c6fb6b6 Mon Sep 17 00:00:00 2001 From: Neeraj310 Date: Thu, 24 Oct 2024 22:20:37 +0545 Subject: [PATCH] feat: add example --- README.md | 135 ++++++++++++++++++++++++++--------------- examples/docker_hub.go | 94 ++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+), 49 deletions(-) create mode 100644 examples/docker_hub.go diff --git a/README.md b/README.md index 48aea13..279549a 100644 --- a/README.md +++ b/README.md @@ -11,61 +11,98 @@ a simple `locator_description` in natural language. Here's a quick example of how to use the project: ```go +/* +This is an example of how to use the locatr package to interact with a webpage using Playwright and an LLM (Large Language Model) client. +In this example, we launch a Chromium browser, navigate to Docker Hub, interact with the search input field by filling in a search term ('Python'), +and perform additional actions like clicking on a Python repository link and navigating to its 'Tags' tab. The locatr package is used for web element +identification, and an LLM client is integrated for contextual descriptions of the locators. Predefined locators are leveraged to simplify interaction +with the webpage elements. +*/ + package main import ( - "log" - "os" - - "github.com/playwright-community/playwright-go" - "github.com/vertexcover-io/locatr/llm" - "github.com/vertexcover-io/locatr/plugins" + "fmt" + "log" + "os" + "time" + + "github.com/playwright-community/playwright-go" + "github.com/vertexcover-io/locatr/core" + "github.com/vertexcover-io/locatr/llm" ) func main() { - pw, err := playwright.Run() - if err != nil { - log.Fatalf("could not start playwright: %v", err) - } - defer pw.Stop() - - browser, err := pw.Chromium.Launch() - if err != nil { - log.Fatalf("could not launch browser: %v", err) - } - defer browser.Close() - - page, err := browser.NewPage() - if err != nil { - log.Fatalf("could not create page: %v", err) - } - if _, err := page.Goto("https://www.youtube.com/"); err != nil { - log.Fatalf("could not navigate to YouTube: %v", err) - } - - llmClient, err := llm.NewLlmClient( - os.Getenv("LLM_PROVIDER"), // (openai | anthropic), - os.Getenv("LLM_MODEL_NAME"), - os.Getenv("LLM_API_KEY"), - ) - if err != nil { - log.Fatalf("could not create llm client: %v", err) - } - - locatr := plugins.NewPlaywrightLocatr(page, llmClient) - - searchBarLocator, err := locatr.GetLocatr("search bar") - if err != nil { - log.Fatalf("could not get locator: %v", err) - } - if err = searchBarLocator.Click(); err != nil { - log.Fatalf("could not click search bar: %v", err) - } + pw, err := playwright.Run() + if err != nil { + log.Fatalf("could not start playwright: %v", err) + } + defer pw.Stop() + + browser, err := pw.Chromium.Launch( + playwright.BrowserTypeLaunchOptions{ + Headless: playwright.Bool(false), + }, + ) + if err != nil { + log.Fatalf("could not launch browser: %v", err) + } + defer browser.Close() + + page, err := browser.NewPage() + if err != nil { + log.Fatalf("could not create page: %v", err) + } + if _, err := page.Goto("https://hub.docker.com/"); err != nil { + log.Fatalf("could not navigate to docker hub: %v", err) + } + time.Sleep(5 * time.Second) // wait for page to load + + llmClient, err := llm.NewLlmClient( + os.Getenv("LLM_PROVIDER"), // (openai | anthropic), + os.Getenv("LLM_MODEL_NAME"), + os.Getenv("LLM_API_KEY"), + ) + if err != nil { + log.Fatalf("could not create llm client: %v", err) + } + options := core.BaseLocatrOptions{UseCache: true} + + locatr := core.NewPlaywrightLocatr(page, llmClient, options) + + searchBarLocator, err := locatr.GetLocatr("Search Docker Hub input field") + if err != nil { + log.Fatalf("could not get locator: %v", err) + } + fmt.Println(searchBarLocator.InnerHTML()) + stringToSend := "Python" + err = searchBarLocator.Fill(stringToSend) + if err != nil { + log.Fatalf("could not fill search bar: %v", err) + } + searchBarLocator.Press("Enter") + time.Sleep(5 * time.Second) + pythonLink, err := locatr.GetLocatr("Link to python repo on docker hub. It has the following description: 'Python is an interpreted, interactive, object-oriented, open-source programming language.'") + if err != nil { + log.Fatalf("could not get locator: %v", err) + } + log.Println("Clicking on python link") + err = pythonLink.First().Click() + if err != nil { + log.Fatalf("could not click on python link: %v", err) + } + time.Sleep(3 * time.Second) + tagsLocator, err := locatr.GetLocatr("Tags tab on the page. It is made up of anchor tag") + if err != nil { + log.Fatalf("could not get locator: %v", err) + } + log.Println("Clicking on tags locator") + err = tagsLocator.Nth(2).Click() + if err != nil { + log.Fatalf("could not click on tags locator: %v", err) + } + time.Sleep(3 * time.Second) } -``` -## Future Plans - -- **Multi Language Support**: Compile the core `BaseLocatr` to **wasm** and use **wasmer** for using `Locatr` in multiple languages. -- **Locator Caching**: Cache the locators so that llm won't be called when running the scraper everytime. +``` diff --git a/examples/docker_hub.go b/examples/docker_hub.go new file mode 100644 index 0000000..ad8aa81 --- /dev/null +++ b/examples/docker_hub.go @@ -0,0 +1,94 @@ +// nolint + +/* +This is an example of how to use the locatr package to interact with a webpage using Playwright and an LLM (Large Language Model) client. +In this example, we launch a Chromium browser, navigate to Docker Hub, interact with the search input field by filling in a search term ('Python'), +and perform additional actions like clicking on a Python repository link and navigating to its 'Tags' tab. The locatr package is used for web element +identification, and an LLM client is integrated for contextual descriptions of the locators. Predefined locators are leveraged to simplify interaction +with the webpage elements. +*/ +package main + +import ( + "fmt" + _ "fmt" + "log" + "os" + "time" + + "github.com/playwright-community/playwright-go" + "github.com/vertexcover-io/locatr/core" + "github.com/vertexcover-io/locatr/llm" +) + +func main() { + pw, err := playwright.Run() + if err != nil { + log.Fatalf("could not start playwright: %v", err) + } + defer pw.Stop() + + browser, err := pw.Chromium.Launch( + playwright.BrowserTypeLaunchOptions{ + Headless: playwright.Bool(false), + }, + ) + if err != nil { + log.Fatalf("could not launch browser: %v", err) + } + defer browser.Close() + + page, err := browser.NewPage() + if err != nil { + log.Fatalf("could not create page: %v", err) + } + if _, err := page.Goto("https://hub.docker.com/"); err != nil { + log.Fatalf("could not navigate to docker hub: %v", err) + } + time.Sleep(5 * time.Second) // wait for page to load + + llmClient, err := llm.NewLlmClient( + os.Getenv("LLM_PROVIDER"), // (openai | anthropic), + os.Getenv("LLM_MODEL_NAME"), + os.Getenv("LLM_API_KEY"), + ) + if err != nil { + log.Fatalf("could not create llm client: %v", err) + } + options := core.BaseLocatrOptions{UseCache: true} + + locatr := core.NewPlaywrightLocatr(page, llmClient, options) + + searchBarLocator, err := locatr.GetLocatr("Search Docker Hub input field") + if err != nil { + log.Fatalf("could not get locator: %v", err) + } + fmt.Println(searchBarLocator.InnerHTML()) + stringToSend := "Python" + err = searchBarLocator.Fill(stringToSend) + if err != nil { + log.Fatalf("could not fill search bar: %v", err) + } + searchBarLocator.Press("Enter") + time.Sleep(5 * time.Second) + pythonLink, err := locatr.GetLocatr("Link to python repo on docker hub. It has the following description: 'Python is an interpreted, interactive, object-oriented, open-source programming language.'") + if err != nil { + log.Fatalf("could not get locator: %v", err) + } + log.Println("Clicking on python link") + err = pythonLink.First().Click() + if err != nil { + log.Fatalf("could not click on python link: %v", err) + } + time.Sleep(3 * time.Second) + tagsLocator, err := locatr.GetLocatr("Tags tab on the page. It is made up of anchor tag") + if err != nil { + log.Fatalf("could not get locator: %v", err) + } + log.Println("Clicking on tags locator") + err = tagsLocator.Nth(2).Click() + if err != nil { + log.Fatalf("could not click on tags locator: %v", err) + } + time.Sleep(3 * time.Second) +}