[英]How to validate the title of text box in selenium c#
在我的應用程序中,其中一個頁面有更多的下拉框和文本框。我正在嘗試驗證每個文本框和下拉框的標題,但我無法構造正確的 xpath 來進行斷言。
下面是該文本框和下拉框的 HTML 代碼。
文本框和下拉菜單的標題如下:
請 Select 信用類型:
你需要多少?
貸款期限
雇佣類型
<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;"> </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;"> </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;"> </span>
</div>
</div>
我無法構造 xpath 來定位正確的 web 元素來斷言標題,因為 class 名稱對於所有文本框都是相同的。只有“for”屬性被更改。
什么是定位元素的正確方法。請建議合適的 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");
您可以將 webelement 中的任何屬性用作 xpath 的一部分。 在這種情況下,您可以像這樣使用“for”屬性,
By.XPath("//label[@for='creditType']")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.