如何按标签查找元素

时间:2014-04-14 23:33:59

标签: selenium protractor

我有这个带有标签"名字"的div,我想在Selenium e2e测试中找到它:

<div class="control-group ng-class: { 'error': isInErrorState() } ng-scope" dto-control-group="" label="First Name">
    <label class="control-label" for="FirstName">
        <span class="ng-binding">First Name</span>
        <span class="required-asterisk" ng-show="isRequired">*</span>
    </label>
    <div class="controls">
                <input type="text" id="FirstName" dto-input-validate="" name="FirstName" ng-model="qform.submission.persons[0].firstName" class="ng-scope ng-pristine ng-invalid ng-invalid-dtorule">

        <div class="inline-messages">
            <!-- ngIf: helpMsg -->
            <span class="error-inline ng-binding">This is a required field</span>
        </div>
    </div>
</div>

这是我的Java代码:

// XXX this returns NULL
@FindBy(xpath = "//div[@label=\"First Name\"]")
private InputElement firstName;

这里有什么问题?

2 个答案:

答案 0 :(得分:4)

我认为这对你有用:

@FindBy(xpath = "//div[@label='First Name']")

答案 1 :(得分:0)

用户:

.//*[@label='First Name']//*[@class='control-label']

OR

.//div/label