Skip to content

Commit

Permalink
feat: add example
Browse files Browse the repository at this point in the history
  • Loading branch information
Neeraj319 committed Oct 24, 2024
1 parent 10bc856 commit b3f6b8b
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 49 deletions.
135 changes: 86 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
```

94 changes: 94 additions & 0 deletions examples/docker_hub.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit b3f6b8b

Please sign in to comment.