Functions/Get-Puppeteer_ClickOnSelector.ps1

<#
.SYNOPSIS
    This function returns the Node JS Puppeteer code for clicking on a selector on a browser page.
#>

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

        # Select whether to wait for the page to load after clicking
        [Parameter(Mandatory=$false)]
        [Switch]$waitForPageToLoad = [Switch]::Present
    )

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

    $code = $code -replace "%selectorName%", $selectorName
    if ($waitForPageToLoad) {
        $code += @"
await Promise.all([
    page.click(selector),
    page.waitForNavigation()
])
"@

    }
    else {
        $code += @"
await page.click(selector);
"@

    }

    # Return the code
    return $code
}