Balaji Mohan
Balaji Mohan

Reputation: 63

How to validate the title of text box in selenium c#

In my application,one of the page has more dropdown box and text box.I am tring to validate the title of each text box and dropdown box but i am unable to construct proper xpath for doing assertion.

Below is the HTML code of that text box and dropdown box.

The title of the text box and dropdown is as below:

Please Select Credit Type:

How Much Do You Need?

Loan Term

Employment Type

 <div class="row">
                            <div class="col-md-6">
                                <label class="control-label" for="creditType">Please Select Credit Type:</label>
                                <select class="form-control" id="CreditType" name="creditType" required="" onclick="hideError(this)" onchange="onProductSelection()" onfocus="hideError(this)">
                                    <option value="1" id="inputDivMaster-1" selected="">Personal Loan</option>
                                    <option value="2" id="inputDivMaster-2">Credit Card</option>
                                    
                                </select>
                            </div>
                            <div class="col-md-6">
                                <label class="control-label" for="loanAmount">How Much Do You Need?</label>
                                <input type="text" class="form-control allownumericwithoutdecimal" id="loanAmountInput" name="loanAmount" placeholder="R500 - R249 999" pattern="\d*" maxlength="6" onclick="hideErrorById('loanAmount')" value="249999" onfocus="hideErrorById('loanAmount')">
                                <span class="dl-error-msg" id="loanAmountError" style="display: none;">&nbsp;</span>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <label class="control-label" for="loanTerm">Loan Term (Months)</label>
                                <input type="text" class="form-control allownumericwithoutdecimal" id="loanTermInput" name="loanTerm" placeholder="1 - 84 months" pattern="\d*" maxlength="2" onclick="hideErrorById('loanTerm')" value="80" onfocus="hideErrorById('loanTerm')">
                                <span class="dl-error-msg" id="loanTermError" style="display: none;">&nbsp;</span>
                            </div>
                            <div class="col-md-6">
                                <label class="control-label" for="employmentStatus">Employment Type</label>
                                <select class="form-control" id="EmploymentStatusInput" name="employmentStatus" required="" onclick="hideErrorById('EmploymentStatus')" onfocus="hideErrorById('EmploymentStatus')"><option value="1">Permanent</option><option value="2">Temp/Seasonal/Casual worker</option><option value="3">Unemployed</option><option value="4">Pensioner</option><option value="5">Contract Worker</option><option value="6">Self Employed</option></select>
                                <span class="dl-error-msg" id="EmploymentStatusError" style="display: none;">&nbsp;</span>
                            </div>
                        </div>

I am unable to construct xpath for locating the proper webelement to assert the title since the class name is same for all text boxes.Only the 'for' attribute gets changed.

What is the right way to locate the element.Kindly suggest suitable xpath.

String actual_title = driver.FindElement(By.XPath("_____?______")).Text;
            String expected_title = "Please Select Credit Type:";
            Assert.AreEqual(actual_title, expected_title);
            Console.WriteLine("Text Box Title validated successfully");

Upvotes: 1

Views: 587

Answers (1)

Sureshmani Kalirajan
Sureshmani Kalirajan

Reputation: 1938

You can use any attribute from the webelement as part of the xpath. In this case, you can use "for" attribute like this,

By.XPath("//label[@for='creditType']")

Upvotes: 1

Related Questions