Home > Net >  Selenium.WebElement.Text is returning blank using C#
Selenium.WebElement.Text is returning blank using C#

Time:11-26

I am currently trying to obtain some printed text from an automated test website after I have completed a form. The returned values are displayed to the user on the screen after submitting the form, but for some reason I cannot obtain 2 of the 4 text values with Selenium's WebElement, I have tried using .Text and GetAttribute("value"), and they both return blank. Yet the first 2 pieces of text returned, I am able to retrieve. Please see screenshot below along with the code.

Code displaying blank text, along with the test site

//Then I verify the form submitted

Constants.confirmationName = driver.FindElement(By.CssSelector("#name"));
if (Constants.confirmationName.Text == "Name:QA Automation")
{
    Console.WriteLine(Constants.confirmationName.Text);
}
else
{
    Console.WriteLine("We have a different name stored for you.");
}


Constants.confirmationEmail = driver.FindElement(By.CssSelector("#email"));
if (Constants.confirmationEmail.Text == "Email:[email protected]")
{
    Console.WriteLine(Constants.confirmationEmail.Text);
}
else
{
    Console.WriteLine("We have a different email stored for you.");
}

Thread.Sleep(2000);

//NOT WORKING
Constants.confirmationCurrentAddress = driver.FindElement(By.CssSelector("#currentAddress"));
Thread.Sleep(2000);
if (Constants.confirmationCurrentAddress.Text == "Current Address :Cedars 2 ")
{
    Console.WriteLine(Constants.confirmationCurrentAddress.Text);
}
else
{
    Console.WriteLine("We have a different current address stored for you.");
}

//NOT WORKING
Constants.confirmationPermanentAddress = driver.FindElement(By.CssSelector("#permanentAddress"));
if (Constants.confirmationPermanentAddress.Text == "Permananet Address :63 Wheat Drive")
{
    Console.WriteLine(Constants.confirmationPermanentAddress.Text);
}
else
{
    Console.WriteLine("We have a different permanent address stored for you.");
}

The code confirms the name that is printed, and I can see it returned, same goes for the email address, but the current address and permanent address both return blank, I've tried to add in wait times too, but to no avail.

The website in question is Name

You can use the Current Address

Hence, to extract the value of Current Address and Permananet Address instead of using the Text property you need to use GetAttribute("value") method as follows:

  • To extract Current Address:

    Constants.confirmationCurrentAddress = driver.FindElement(By.CssSelector("#currentAddress"));
    if (Constants.confirmationCurrentAddress.GetAttribute("value") == "Current Address :Cedars 2 ")
    {
        Console.WriteLine(Constants.confirmationCurrentAddress.Text);
    }
    
  • To extract Permananet Address:

    Constants.confirmationPermanentAddress = driver.FindElement(By.CssSelector("#permanentAddress"));
    if (Constants.confirmationPermanentAddress.GetAttribute("value") == "Permananet Address :63 Wheat Drive")
    {
        Console.WriteLine(Constants.confirmationPermanentAddress.Text);
    } 
    

CodePudding user response:

You should wait for DOM to load. If the element is somehow hidden, you can get the innerHTML attribute.

  • Related