Forms and Inputs

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:\data\search.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);


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


GPAL.Browser

.WithBrowserType(BrowserType.Edge)

.WithDriverLocation("c:\drivers")

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

.WithSelector(searchSelector)

.CallAfterFillIn(CallAfterFillIn)

.FillInFrom(inputFile)

.Close(true);


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(ref resultGrid)

.SaveTo(outputFile);


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


return CallIfStatus.Handled;

}


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:\data\search.words.txt";


Selector searchSelector = (Selector)GPAL.Selector

.WithSelectorName("SearchInput")

.WithCSS("#gh-ac")

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

.MatchPlaceholder("Search for anything");

NOTE

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.

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(ref resultGrid)

.SaveTo(outputFile);


return CallIfStatus.Handled;

}

WARNING

GPAL moves on to the next token whether CallAfterFillIn returns Handled or NotHandled. Return CallIfStatus.Terminate to stop instead: GPAL publishes that the handler requested termination and ends the workflow after this token.

💬 Ask GPAL