Search code examples
c#seleniumautomationnugetgettext

Missing Get Methods Within Selenium


I may be looking or doing the wrong thing but when I attempt to use the GetAttribute or GetText methods within Selenium they're not present?

so for example, there is a Forgotten Password link which is easy to find using the ID, however, there is text above it with instructions for the users to select the button if they have forgotten their password. I was hoping to use GetText, however thats not in my drop down options; I am only given: GetHashCode, GetType, GetScreenShot. Is it in a Nuget package i dont have or should it be as standard with Selenium?

public IWebElement ForgotPasswordText { get { return _driver.gettext(("Please Click Below To Reset Your Password")); } }

Assert.IsTrue(HomePage.ForgotPasswordText.Displayed);

I have used this before no bother but not in this project and cannot determine the differences to see where ive gone wrong.

The GetText is not present whereas it will usually auto populate with the option.


Solution

  • From what I can tell, you are trying to use GetAttribute and GetText to locate WebElements. These methods can only be used on WebElements objects, that have already been located. To find your ForgotPasswordText element, you can use this:

    public IWebElement ForgotPasswordText { get { return _driver.FindElement(By.XPath("//*[text()='Please Click Below To Reset Your Password']")); } }
    

    This will get you a WebElement with the text 'Please Click Below To Reset Your Password'.

    Now, if you want to actually get the text or attribute of a WebElement, here's how you would do that:

    var webElement = Driver.FindElement(By.XPath("//div[@class='someClass']"));
    
    var text = webElement.Text;
    var classAttribute = webElement.GetAttribute("class");
    var valueAttribute = webElement.GetAttribute("value");