0

Please I am trying to validate error message text in a try catch and it is just catching every time. Need assistance with syntax or better way of the validation

string actualResultText = "";
string expectedResultText = "Error: Please Enter User Name";
IWebElement actualResult = webDriver.FindElement(By.XPath("//*[@id='id-7530880b3e6759b']/li/span[contains(text(),'Error: Please Enter User Name')]"));

actualResultText=actualResult.ToString();

if (actualResultText == expectedResultText)
{
    result = true;
}
    else
{
    result = false;
}

Inspect Please view this inspect against code

Mattias
  • 3,907
  • 4
  • 28
  • 50

1 Answers1

0

I'm working with selenium in python, but the answer might have something similar in C#.
In python, in order to get the text of an element you use:

element = driver.find_element(by, value)  # get the element
print(element.text)  # get the text of the element

The difference between here and what you are doing (again, if C# has something similar and it works the way I think it is) is using the attribute of the element object text, rather than converting the element object into a string.

RonKon
  • 133
  • 8
  • Thank you so much for the suggestion. The issue I have is related to the IWebElement syntax to find the element correctly. I did post the inspect as well to see if anyone can show/tell me where I am in error. I get kicked to the catch block after IWebElement line. – Keith Moon Oct 19 '21 at 16:38
  • If I understand correctly, you wish to find an element based on the text presented. If so, please go to https://stackoverflow.com/a/18701085/14947575 – RonKon Oct 20 '21 at 07:14