I did a little research here and here, however when I apply the given examples with this random url: https://www.ibm.com/support/pages/it-possible-use-upper-or-lowercase-function-xpath-expression I get the error below with Selenium WebDriver for .NET:
OpenQA.Selenium.StaleElementReferenceException: 'The element reference of {script src="https://example.com/"} is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed'
My purpose is to get all the elements on the current web-page in this universal function:
Function FindElementsByText(drv As IWebDriver, text As String,
stringComparison As StringComparison) As ReadOnlyCollection(Of IWebElement)
Return (From element As IWebElement In drv.FindElements(By.CssSelector("*"))
Where element.Text.Equals(text, stringComparison)
).ToList().AsReadOnly()
End Function
Where drv.FindElements(By.CssSelector("*")) (or drv.FindElementsByXPath("//*")) is what is causing the exception above after I instruct the driver to navigate to the url that I specified at the beginning.
The function above would be a great improvement for this other universal function limited by the translate function of XPath 1.0:
Function FindElementsByText(drv As RemoteWebDriver, text As String,
ignoreCase As Boolean) As ReadOnlyCollection(Of IWebElement)
Const translateUpperText As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZÁÉÍÓÚÀÈÌÒÙÄËÏÖÜÂÊÎÔÛÑÇ"
Const translateLowerText As String = "abcdefghijklmnopqrstuvwxyzáéíóúàèìòùäëïöüâêîôûñç"
If ignoreCase Then
Return drv.FindElementsByXPath($"//*[contains(translate(text(), {translateUpperText}, {translateLowerText}),'{text.ToLower()}')]")
Else
Return drv.FindElementsByXPath("//*[contains(text(),'{text}')]")
End If
End Function
My question is: how can I get rid of the error I'm getting to get all the elements in a web-page in the proper way?.