Forms and Inputs

Driving a Search Form from a Database Table

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;


IGPALGrid<string> retGrid = GPAL.Grid.ToGPALObject();

int fileNameIdx = 0;


GPALDatabase database = (GPALDatabase)GPAL.Database

.WithServerName("MYSERVER\SQLEXPRESS")

.WithDatabaseName("GPAL")

.WithDatabaseType(DatabaseType.SQLServer)

.WithTableName("TestInput")

.WithRowCount(4);


retGrid = GPAL.Grid.ToGPALObject();


Selector searchInput = (Selector)GPAL.Selector

.WithSelectorName("SearchInput")

.WithCSS("#gh-ac")

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

.MatchPlaceholder("Search for anything");


IBrowser browser = GPAL.Browser

.WithBrowserType(BrowserType.FireFox)

.WithDriverLocation("c:\drivers")

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

.WithSelector(searchInput)

.CallAfterFillIn(CallAfterFillIn)

.ToGPALObject();


browser.FillInFrom(database);


browser.Close(true);


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

{

GPALFile fileList = $"c:\temp\results{fileNameIdx++}.txt";


Selector searchButton = (Selector)GPAL.Selector

.WithSelectorName("SearchButton")

.WithCSS("#gh-btn")

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

.MatchText("Search");


Selector descriptionColumn = (Selector)GPAL.Selector

.WithSelectorName("Description")

.WithCSS("#srp-river-results > ul > li > div > div.s-item__info > a > h3");


Selector priceColumn = (Selector)GPAL.Selector

.WithSelectorName("Price")

.WithCSS("#srp-river-results > ul > li > div > div.s-item__info > div > div:nth-child(1) > span");


browser

.WithSelector(searchButton)

.LeftClick()

.WithSelector(descriptionColumn)

.WithSelector(priceColumn)

.WithAllThatMatch()

.GetGrid(ref retGrid)

.SaveTo(fileList);


return CallIfStatus.Handled;

}

Describe the Source Table with GPALDatabase

GPAL.Database is a fluent factory just like GPAL.Browser. WithServerName, WithDatabaseName, and WithDatabaseType point it at a SQL Server instance, WithTableName picks the table to read from, and WithRowCount caps how many rows the workflow will process.

GPALDatabase database = (GPALDatabase)GPAL.Database

.WithServerName("MYSERVER\SQLEXPRESS")

.WithDatabaseName("GPAL")

.WithDatabaseType(DatabaseType.SQLServer)

.WithTableName("TestInput")

.WithRowCount(4);

NOTE

Like GPALUrl and GPALFile, GPALDatabase carries its configuration with it - build it once and pass it directly into FillInFrom rather than opening a connection yourself. See GPALDatabase: Querying and Writing SQL.

Target the Input and Register CallAfterFillIn

WithSelector points at the search box, and CallAfterFillIn registers a callback that GPAL will run after each value from the database is typed in. The callback receives the grid of values being iterated (tokens) and the current row index (tokenIdx).

Selector searchInput = (Selector)GPAL.Selector

.WithSelectorName("SearchInput")

.WithCSS("#gh-ac")

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

.MatchPlaceholder("Search for anything");


IBrowser browser = GPAL.Browser

.WithBrowserType(BrowserType.FireFox)

.WithDriverLocation("c:\drivers")

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

.WithSelector(searchInput)

.CallAfterFillIn(CallAfterFillIn)

.ToGPALObject();

FillInFrom(database) Drives the Loop

Passing the GPALDatabase object to FillInFrom tells GPAL to query the table, then for each row, fill the targeted selector with that row's value and fire CallAfterFillIn. One call replaces a manual loop over query results plus a manual fill-and-search for each one.

browser.FillInFrom(database);


browser.Close(true);

WARNING

FillInFrom(database) blocks until every row has been processed and CallAfterFillIn has run for each one - Close(true) only fires once the entire database-driven loop finishes.

CallAfterFillIn: Search, Scrape, and Save Per Row

Inside the callback, the workflow clicks the search button, collects description and price columns from every matching result with WithAllThatMatch, pulls them into a grid with GetGrid, and writes each row's results to its own tabbed text file using a counter to keep filenames unique.

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

{

GPALFile fileList = $"c:\temp\results{fileNameIdx++}.txt";


browser

.WithSelector(searchButton)

.LeftClick()

.WithSelector(descriptionColumn)

.WithSelector(priceColumn)

.WithAllThatMatch()

.GetGrid(ref retGrid)

.SaveTo(fileList);


return CallIfStatus.Handled;

}

NOTE

CallAfterFillIn must return a CallIfStatus. GPAL moves on to the next row from the database whether it gets Handled or NotHandled; only Terminate stops the loop. See Data-Driven Forms: FillInFrom and CallAfterFillIn.

💬 Ask GPAL