powershellselenium-webdrivermicrosoft-edgeselenium-edgedriver

Powershell script causing Selenium Edge WebDriver's 'FindElementBy' method error


I would like to automate a website. First there is a login page and then I want to navigate through the website by clicking two buttons. However, due to internal guidelines, I can only use the Edge browser and Powershell. The script was already running, but I always get the same error message after a change. Unfortunately, I no longer remember the change.

The error message:


Error calling method because [OpenQA.Selenium.Edge.EdgeDriver] does not contain a method named "FindElementByXPath". In //PATH $browser.FindElementByXPath('/html/body/div[4]/center/table/tbody/tr/ ... CategoryInfo : InvalidOperation: (:) [], RuntimeException FullyQualifiedErrorId : MethodNotFound


I am very grateful for quick help. The file is on a local storage.

function Create-Browser {
    param(
        [Parameter(mandatory=$false)][ValidateSet('Chrome','Edge','Firefox')][string]$browser,
        [Parameter(mandatory=$false)][bool]$HideCommandPrompt = $true,
        [Parameter(mandatory=$false)][string]$driverversion = ''
    )
    $driver = $null
    
    function Load-NugetAssembly {
        [CmdletBinding()]
        param(
            [string]$url,
            [string]$name,
            [string]$zipinternalpath,
            [switch]$downloadonly
        )
        if($psscriptroot -ne ''){
            $localpath = join-path $psscriptroot $name
        }else{
            $localpath = join-path $env:TEMP $name
        }
        $tmp = "$env:TEMP\$([IO.Path]::GetRandomFileName())"
        $zip = $null
        try{
            if(!(Test-Path $localpath)){
                Add-Type -A System.IO.Compression.FileSystem
                write-host "Downloading and extracting required library '$name' ... " -F Green -NoNewline
                (New-Object System.Net.WebClient).DownloadFile($url, $tmp)
                $zip = [System.IO.Compression.ZipFile]::OpenRead($tmp)
                $zip.Entries | ?{$_.Fullname -eq $zipinternalpath} | %{
                    [System.IO.Compression.ZipFileExtensions]::ExtractToFile($_,$localpath)
                }
                Unblock-File -Path $localpath
                write-host "OK" -F Green
            }
            if(!$downloadonly.IsPresent){
                Add-Type -LiteralPath $localpath -EA Stop
            }
        
        }catch{
            throw "Error: $($_.Exception.Message)"
        }finally{
            if ($zip){$zip.Dispose()}
            if(Test-Path $tmp){del $tmp -Force -EA 0}
        }  
    }

    # Load Selenium Webdriver .NET Assembly
    Load-NugetAssembly 'https://www.nuget.org/api/v2/package/Selenium.WebDriver' -name 'WebDriver.dll' -zipinternalpath 'lib/net45/WebDriver.dll' -EA Stop

    if($psscriptroot -ne ''){
        $driverpath = $psscriptroot + '\'
    }else{
        $driverpath = $env:TEMP
    }

    Load-NugetAssembly "https://www.nuget.org/api/v2/package/Selenium.WebDriver.MSEdgeDriver/$driverversion" -name 'msedgedriver.exe' -zipinternalpath 'driver/win64/msedgedriver.exe' -downloadonly -EA Stop

    #msedge driver laden
    # create driver service
    $dService = [OpenQA.Selenium.Edge.EdgeDriverService]::CreateDefaultService($driverpath)
    # hide command prompt window
    $dService.HideCommandPromptWindow = $HideCommandPrompt
    
    # create driver object
    $driver = New-Object OpenQA.Selenium.Edge.EdgeDriver $dService

return $driver
}


$browser= Create-Browser

$browser.Manage().Window.Maximize()

$browser.Navigate().GotoURL("//INTERNAL URL")

$browser.FindElementByXPath('/html/body/div[4]/center/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr/td[2]/form/table/tbody/tr[1]/td/input').SendKeys('//USER')

$browser.FindElementByXPath('/html/body/div[4]/center/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr/td[2]/form/table/tbody/tr[2]/td/input').SendKeys('PASSWORD')

$browser.FindElementByXPath('/html/body/div[4]/center/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr/td[2]/form/table/tbody/tr[3]/td/input').Click()
Start-Sleep 1

$browser.FindElementByXPath('/html/body/div[1]/ul[1]/li[4]/a').Click()

$browser.FindElementByXPath('/html/body/div[4]/table/tbody/tr[19]/td[2]/a').Click()

$browser.quit()


I have already changed the code without success, deleted the downloaded files, asked ChatGPT and restarted the PC


Solution

  • You need to specify the fully qualified class name of By as follows:

    $driver.FindElement([OpenQA.Selenium.By]::XPath("xpath_expression"))
    

    The :: accessor is necessary as the XPath() method is a static member of the By class.