在xpath中找到self或perent节点的兄弟元素

时间:2017-06-16 10:44:30

标签: selenium xpath selenium-webdriver

我试图找到一个可以在自身节点或其父节点的兄弟之前的元素。

我必须得到所有文本字段值以及为它们提供的标签,如下图所示:

enter image description here

如果我看到DOM,我发现了按照

中提到的结构

enter image description here

我试过下面的代码

func resolutionSizeForAsset(_ asset: AVAsset) -> CGSize? {
        guard let track = asset.tracks(withMediaType: AVMediaTypeVideo).first else { return nil }
        let size = track.naturalSize.applying(track.preferredTransform)
        return CGSize(width: fabs(size.width), height: fabs(size.height))
    }

它适用于 // this is the xpath to get all the textboxes @FindBy(xpath = "//input[not(@id='fuPanCardFilePath')][not(@type='hidden')][not(@id='fuIDProofFilePath' or @id='txtChangePhone' or @id='txtOTP' )]") public List<WebElement> allTextFieldsDetails; // This how i am getting labels of correspondent textfield for(WebElement e : allTextFieldsDetails) { LogWriter.logger.info(e.findElement(By.xpath("preceding-sibling::label")).getText() +"---"+e.getAttribute("value")); } 的{​​{1}},它是<label>的确切前兄弟,但如果<input>label的父div的先前兄弟input然后它抛出异常NoSuchElementException

这是HTML结构:

<div class="row">
    <div class="col-sm-6 form-group">
        <label>Last Name </label>
        <input id="LastName" class="form-control valid" data-val="true" data-val-length="The field Last Name must be a string with a maximum length of 20." data-val-length-max="20" data-val-regex="Please enter valid Last Name" data-val-regex-pattern="[a-zA-Z ]+" data-val-required="Please enter valid Last Name" maxlength="20" name="LastName" placeholder="Last Name" value="Kapadia" aria-required="true" aria-describedby="LastName-error" aria-invalid="false" type="text"/>
        <span class="field-validation-valid" data-valmsg-for="LastName" data-valmsg-replace="true"/>
    </div>
</div>
<div class="row">
    <div class="col-sm-6 form-group">
        <label>Email ID</label>
        <input id="Email" class="form-control valid" data-val="true" data-val-length="The field Email must be a string with a maximum length of 320." data-val-length-max="320" data-val-regex="Please enter valid Email" data-val-regex-pattern="[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,3}$" data-val-required="Please enter valid Email" maxlength="80" name="Email" placeholder="Email" value="narendra.h.rajput@trimantra.net" aria-required="true" aria-describedby="Email-error" aria-invalid="false" type="email"/>
        <span class="field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"/>
    </div>
</div>
<div class="col-sm-6 form-group">
        <label>Date Of Birth</label>
        <div class="input-append date tspDOB-date">
            <input id="DOB" class="form-control" value="1989-09-09" data-val="true" data-val-date="The field Date of Birth must be a date." data-val-required="Please enter a valid birth date" name="DOB" placeholder="Date of Birth" type="text"/>
            <span class="field-validation-valid" data-valmsg-for="DOB" data-valmsg-replace="true"/>
            <span class="add-on"/>
        </div>
</div>
<div class="row">
    <div class="col-sm-6 form-group">
        <label>Mobile No</label>
        <div class="col-sm-12 form-group displayinlinediv">
            <input id="Mobile" class="form-control valid" name="Mobile" readonly="readonly" value="00000000000" aria-invalid="false" type="text"/>
            <button id="btnchange" class="btn btn-common-left" type="button">Change</button>
        </div>
    </div>
</div>

我想找到与输入字段对应的标签。

2 个答案:

答案 0 :(得分:2)

你可以尝试

preceding::label[1]

获取第一个label元素,它可以是父母或祖父母的兄弟或兄弟...

另请注意,您可以将XPath元素的input简化为

//input[not(@type='hidden') and not(@id=('fuIDProofFilePath', 'txtChangePhone', 'txtOTP', 'fuPanCardFilePath'))]

答案 1 :(得分:0)

您可以使用(.|..)/preceding-sibling::label='xyz'

请注意,如果你的意思是立即在兄弟姐妹之前,那么它应该是preceding-sibling::label[1]