【问题标题】:How to validate the title of text box in selenium c#如何在 selenium c# 中验证文本框的标题
【发布时间】:2020-08-20 13:39:59
【问题描述】:

在我的应用程序中,其中一个页面有更多的下拉框和文本框。我正在尝试验证每个文本框和下拉框的标题,但我无法构造正确的 xpath 来进行断言。

下面是该文本框和下拉框的 HTML 代码。

文本框和下拉菜单的标题如下:

请选择信用类型:

你需要多少?

贷款期限

就业类型

 <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>

我无法构造 xpath 来定位正确的 web 元素以断言标题,因为所有文本框的类名都相同。只有“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");

【问题讨论】:

  • ("//label[text()='请选择信用类型:']")

标签: c# selenium unit-testing xpath automated-tests


【解决方案1】:

您可以使用来自 web 元素的任何属性作为 xpath 的一部分。在这种情况下,您可以像这样使用“for”属性,

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多