Tutorials

Tutorials

FillInFrom doesn't just take a string - point it at a GPALFile and GPAL treats each line as a token to type, run, and react to in turn. This tutorial searches a site once per line in a text file, using CallAfterFillIn to scrape and save results for each search term.

Complete Program

Here's the whole workflow, start to finish. Each piece is broken down and explained below.

using GenerallyPositive;

using GenerallyPositive.Browser;

using static GenerallyPositive.Enums;

using System;

using System.Diagnostics;

GPALFile inputFile = @"c:datasearch.words.txt";

Selector searchSelector = (Selector)GPAL.Selector

.WithSelectorName("SearchInput")

.WithCSS("#gh-ac")

.WithXPath("//*[@id='gh-ac']")

.MatchPlaceholder("Search for anything");

GPAL.WithExceptionHandler(ExceptionEventHandler)

.WithInformationHandler(ExceptionEventHandler)

.WithTypingDelay(50);

GPAL.Browser

.WithBrowserType(BrowserType.Edge)

.WithDriverLocation(@"c:drivers")

.GoTo("https://www.example.com/")

.WithSelector(searchSelector)

.CallAfterFillIn(CallAfterFillIn)

.FillInFrom(inputFile)

.Close(true);

static IGPALGrid<string> resultGrid;

static CallIfStatus CallAfterFillIn(IBrowser myBrowser, IGPALGrid<string> tokens, int tokenIdx)

{

GPALFile outputFile = $@"c:data{tokens[tokenIdx][0]}.txt";

Selector searchButton = (Selector)GPAL.Selector

.WithSelectorName("SearchButton")

.WithCSS("#gh-btn")

.MatchText("Search");

Selector descriptionColumn = (Selector)GPAL.Selector

.WithSelectorName("Description")

.WithCSS("li .item-title");

Selector priceColumn = (Selector)GPAL.Selector

.WithSelectorName("Price")

.WithCSS("li .item-price");

myBrowser

.WithSelector(searchButton)

.LeftClick()

.WithSelector(descriptionColumn)

.WithSelector(priceColumn)

.WithAllThatMatch()

.GetGrid(out resultGrid)

.SaveToTabbedText(outputFile);

Debug.WriteLine($"File saved to {outputFile.ReturnFilenames[0]}.");

return CallIfStatus.Handled;

}

static void ExceptionEventHandler(object sender, EventArgs e)

{

Debug.WriteLine(((GPAL.GPALEventArgs)e).Message);

Debug.WriteLine(((GPAL.GPALEventArgs)e).ExceptionRaised?.Message);

}

A GPALFile of Search Terms

Assigning a path string to a GPALFile gives you a value object, not just a filename. Pass it to FillInFrom and GPAL reads the file, treats each line (or token) as one value to type into the selected element, and runs the rest of the chain once per token.

GPALFile inputFile = @"c:datasearch.words.txt";

Selector searchSelector = (Selector)GPAL.Selector

.WithSelectorName("SearchInput")

.WithCSS("#gh-ac")

.WithXPath("//*[@id='gh-ac']")

.MatchPlaceholder("Search for anything");

TIP

FillInFrom can take a literal string, a GPALFile, or (as in the GPALDatabase tutorial) a database query result. GPAL treats all three the same way: a source of tokens to type one at a time.

CallAfterFillIn Runs Once Per Token

FillInFrom drives the loop; CallAfterFillIn is your hook into each iteration. tokens[tokenIdx][0] gives you the search term that was just typed, which is used here to name the output file after the search.

GPAL.Browser

.WithBrowserType(BrowserType.Edge)

.WithDriverLocation(@"c:drivers")

.GoTo("https://www.example.com/")

.WithSelector(searchSelector)

.CallAfterFillIn(CallAfterFillIn)

.FillInFrom(inputFile)

.Close(true);

Click Search, Scrape, Save - Per Term

Inside the callback, build a fresh chain: click the search button, collect description and price columns for every matching result with WithAllThatMatch, pull them into a grid, and write that grid to a file named for the current search term.

static CallIfStatus CallAfterFillIn(IBrowser myBrowser, IGPALGrid<string> tokens, int tokenIdx)

{

GPALFile outputFile = $@"c:data{tokens[tokenIdx][0]}.txt";

Selector searchButton = (Selector)GPAL.Selector

.WithSelectorName("SearchButton")

.WithCSS("#gh-btn")

.MatchText("Search");

Selector descriptionColumn = (Selector)GPAL.Selector

.WithSelectorName("Description")

.WithCSS("li .item-title");

Selector priceColumn = (Selector)GPAL.Selector

.WithSelectorName("Price")

.WithCSS("li .item-price");

myBrowser

.WithSelector(searchButton)

.LeftClick()

.WithSelector(descriptionColumn)

.WithSelector(priceColumn)

.WithAllThatMatch()

.GetGrid(out resultGrid)

.SaveToTabbedText(outputFile);

return CallIfStatus.Handled;

}

WARNING

CallIfStatus.Handled signals that your callback fully processed this iteration. Returning NotHandled lets GPAL fall back to its own default behavior for the token, which is rarely what you want once you've taken over the chain.