Q:

Pls how to access message in span tag

<html>
<body>
<section id="click" class="content mCustomScrollbar _mCS_1">
<div id="mCSB_1" class="mCustomScrollBox mCS-dark" style="position:relative; height:100%; overflow:hidden; max-width:100%;">
<div class="mCSB_container" style="position:relative; top:0;">
<form id="registration" action="http://abcd.com/setup/user-registration/index" method="post" name="registration">
<article class="frm-content">
<div class="leftsection">
<div class="form-element">
<fieldset>
<label>
<input id="firstname" type="text" maxlength="50" value="" name="firstname" style="border: 1px solid red;">
    <label>
<span class="required">*</span>
First Name:
</label>
<input id="firstname" type="text" maxlength="50" value="" name="firstname"     style="border: 1px solid red;">
<span id="error_firstname" class="errormsg" role="alert">Please use only letters,numbers, period, single space and underscore</span>
</fieldset>
</article>
</div>
</section>
</body>
</html>

I Above code I want to find Please use only letters,numbers, period, single space and underscore message. The xpath for that is /html/body/section[2]/div/div/form/article/div/div/span but it is not accessing by gettext() method. Pls how to access this ????

A:

getText() method  fetches only visible (i.e. not hidden by CSS) innerText of respective element. Which might be a cause for the problem. 
Please try the following to check if element is displayed.
Driver.FindElement(By.id("error_firstname")).isDisplayed();

You can add WebDriverwait till that element is displayed and then fetch the text from that element.
WebDriverWait Driver_Wait=new WebDriverWait(Driver, 10); //10 denotes Total_Secs to wait
Driver_Wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("error_firstname")));

Hope this helps.

