Functions/Get-Puppeteer_SetValueForSelector.ps1

<#
.SYNOPSIS
    This function returns the Node JS Puppeteer code for setting a value for a selector on a browser page.
    A typical usage of this function is to select a value from a drop-down list,
    where selectorName is the selector for drop-down list (<select> </select> pair),
    and value is the 'value' attribute in <option>.
#>

function Get-Puppeteer_SetValueForSelector {
    [CmdletBinding(PositionalBinding=$true)]
    [OutputType([String])]
    param (
        # The name of the selector.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$selectorName,

        # The value to set.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$value
    )

    # Generate the code
    $code = @"
selector = '%selectorName%';
await page.waitForSelector(selector);
await page.select(selector, '%value%');
"@

    $code = $code -replace "%selectorName%", $selectorName
    $code = $code -replace "%value%", $value

    # Return the code
    return $code
}