[SOLVED] why i can’t find this web element if i’m using ssh?

Issue

This Content is from Stack Overflow. Question asked by جواد محمد

when i run same script on local machine (windows server 2019) I’m able to find the element, but when i run same script remotely (ssh from my local machine to the windows server) i get element is not found how come? i know this is not issue of graphics because i have the same snippet different element all over my script and they work fine here is the snippet

$searchInput="/html/body/bi-webconsole/ui-view/bi-group-details/div/layout-advanced-details/div/div[2]/div/layout-transclude-view-details/div/bi-group-details-view-smartgroups/div/wc-grid-viewer/div[2]/div/ui-filter-bar/ui-filter-bar-textbox/div/ui-textbox/ui-input-wrapper/div[1]/div/input"
Click-Elment -driver $driver -ifClickable -selector $searchInput     
$searchInput=Wait-ElmentCondition -selector $searchInput -ifClickable
write-debug "[Update-SmartRuleAccessUI]this should be webElment`nsearchInput`n$($searchInput | fl *| out-string)"
$searchInput.Clear()
$searchInput.SendKeys($smartRuleName)
$searchInput.SendKeys([OpenQA.Selenium.Keys]::Enter)

function Wait-ElmentCondition {
    param (
        [string]
        $selector,
        [switch]
        $ifVisable,
        [switch]
        $ifClickable
    )
    if ($ifVisable) {
        Write-Debug "proccessing $selector"
        try {
            [OpenQA.Selenium.Support.UI.WebDriverWait]$wait = New-Object OpenQA.Selenium.Support.UI.WebDriverWait ($driver, [System.TimeSpan]::FromSeconds(5)) 
            $res = $wait.Until([OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementExists([OpenQA.Selenium.By]::XPath(($selector))))
            Write-Debug "[Wait-ElmentCondition]`$res = $res"
            return $res    
            }
        catch {
        }
    }
    if ($ifClickable){
        Write-Debug "proccessing $selector"
        try {
            [OpenQA.Selenium.Support.UI.WebDriverWait]$wait = New-Object OpenQA.Selenium.Support.UI.WebDriverWait ($driver, [System.TimeSpan]::FromSeconds(5))
            $res = $wait.Until([OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementToBeClickable([OpenQA.Selenium.By]::XPath(($selector))))
            Write-Debug "[Wait-ElmentCondition]`$res = $res"
            return $res
            }
        catch {
        }
    }
}

here is the relevant debugs from local session (directly on server)

DEBUG: [Update-SmartRuleAccessUI]this should be webElment
searchInput

WrappedDriver                        : OpenQA.Selenium.Chrome.ChromeDriver
TagName                              : input
Text                                 :
Enabled                              : True
Selected                             : False
Location                             : {X=520,Y=417}
Size                                 : {Width=194, Height=20}
Displayed                            : True
LocationOnScreenOnceScrolledIntoView : {X=520,Y=417}
Coordinates                          : OpenQA.Selenium.Remote.RemoteCoordinates

here is the relevant debug from my ssh session(from my local machine to the windows server)

DEBUG: [Update-SmartRuleAccessUI]this should be webElment
searchInput

InvalidOperation: C:UsersxxDownloadsse1pammange.ps1:836
Line |
 836 |          $searchInput.Clear()
     |          ~~~~~~~~~~~~~~~~~~~~
     | You cannot call a method on a null-valued expression.

InvalidOperation: C:UsersxxDownloadsse1pammange.ps1:837
Line |
 837 |          $searchInput.SendKeys($smartRuleName)
     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | You cannot call a method on a null-valued expression.

InvalidOperation: C:UsersxxDownloadsse1pammange.ps1:838
Line |
 838 |          $searchInput.SendKeys([OpenQA.Selenium.Keys]::Enter)
     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | You cannot call a method on a null-valued expression.

thanks in advance for the help



Solution

you are calling the function before it is defined. moving the function before calling it should fix the issue.

function Wait-ElmentCondition {
    param (
        [string]
        $selector,
        [switch]
        $ifVisable,
        [switch]
        $ifClickable
    )
    if ($ifVisable) {
        Write-Debug "proccessing $selector"
        try {
            [OpenQA.Selenium.Support.UI.WebDriverWait]$wait = New-Object OpenQA.Selenium.Support.UI.WebDriverWait ($driver, [System.TimeSpan]::FromSeconds(5)) 
            $res = $wait.Until([OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementExists([OpenQA.Selenium.By]::XPath(($selector))))
            Write-Debug "[Wait-ElmentCondition]`$res = $res"
            return $res    
            }
        catch {
        }
    }
    if ($ifClickable){
        Write-Debug "proccessing $selector"
        try {
            [OpenQA.Selenium.Support.UI.WebDriverWait]$wait = New-Object OpenQA.Selenium.Support.UI.WebDriverWait ($driver, [System.TimeSpan]::FromSeconds(5))
            $res = $wait.Until([OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementToBeClickable([OpenQA.Selenium.By]::XPath(($selector))))
            Write-Debug "[Wait-ElmentCondition]`$res = $res"
            return $res
            }
        catch {
        }
    }
}

$searchInput="/html/body/bi-webconsole/ui-view/bi-group-details/div/layout-advanced-details/div/div[2]/div/layout-transclude-view-details/div/bi-group-details-view-smartgroups/div/wc-grid-viewer/div[2]/div/ui-filter-bar/ui-filter-bar-textbox/div/ui-textbox/ui-input-wrapper/div[1]/div/input"
Click-Elment -driver $driver -ifClickable -selector $searchInput     
$searchInput=Wait-ElmentCondition -selector $searchInput -ifClickable
write-debug "[Update-SmartRuleAccessUI]this should be webElment`nsearchInput`n$($searchInput | fl *| out-string)"
$searchInput.Clear()
$searchInput.SendKeys($smartRuleName)
$searchInput.SendKeys([OpenQA.Selenium.Keys]::Enter)


This Question was asked in StackOverflow by جواد محمد and Answered by Guenther Schmitz It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?