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.
//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:automation@hotmail.com")
{
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 https://demoqa.com/text-box, if you fill in the fields, then click Submit, you will see the printed information underneath.
Any help would be greatly appreciated as it's driving me insane!
Check the HTML for any other Elements that share the same Id of currentAddress and permanentAddress, I reckon this is the problem as it's likely finding that first element matching that Id and that element has no text (hence the empty string and no exception).
Your code looks fine to me, and the fact that email and name work correctly suggests your code is also fine.
try changing this
Constants.confirmationCurrentAddress = driver.FindElement(By.CssSelector("#currentAddress"));
to
Constants.confirmationCurrentAddress = driver.FindElement(By.CssSelector("p#currentAddress"));
that should help narrow down and find the element you're looking for.