Browser Automation

Driving Native Dropdown Menus with SelectClick

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;


GPAL

.WithUseOttoMagic(@"C:OttoMagic")

.WithAutoUpdateWebDriver();


// the last <option> in eBay's category <select> - "Everything Else"

Selector categoryOption = GPAL.Selector

.WithCSS("#gh-cat > option:nth-child(36)")

.WithHardware

.ToGPALObject();


IBrowser browser = GPAL.Browser

.WithBrowserType(BrowserType.Chrome)

.WithDriverLocation(@"c:drivers")

.WithUseAutomationEngine(AutomationEngine.OttoMagic)

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

.ToGPALObject();


browser

.WithSelector(categoryOption)

.SelectClick(SelectClickType.LeftClick) // jump straight to "Everything Else" (the last option)

.WaitFor(5_000)

.SelectClick(SelectClickType.SelectRandom) // pick a random category

.WaitFor(5_000)

.SelectClick(SelectClickType.SelectNext) // move to the next option

.WaitFor(5_000)

.SelectClick(SelectClickType.SelectPrevious) // back to the previous option

.WaitFor(5_000)

.LeftClick(categoryOption) // re-select "Everything Else" so we know exactly where we are

.WaitFor(5_000)

.SelectClick(SelectClickType.SelectNextWithWrap) // already on the last option, so this wraps to the first ("All")

.WaitFor(5_000)

.SelectClick(SelectClickType.SelectPreviousWithWrap) // wrap back down to the last option ("Everything Else")

.WaitFor(5_000)

.Close(true);

Target an <option> with a Selector

We point a Selector at one specific <option> inside eBay's category dropdown - the last entry, "Everything Else" - using a plain CSS path. SelectClick can then operate on that <option> regardless of how the click is actually carried out under the hood.

// the last <option> in eBay's category <select> - "Everything Else"

Selector categoryOption = GPAL.Selector

.WithCSS("#gh-cat > option:nth-child(36)")

.WithHardware

.ToGPALObject();

NOTE

Every automation engine has its own technique for working with a

Get Ask GPAL companion for your desktop here