0

I have some form:

<div class="form_item--2c8WB">
    <label>
        <span class="label--2VxxL required--2nkmI">
            "Text"
            ::after
        </span> 
        <br> 
        <input type="password" name="newPasswordRepeat" autocomplete="new-password" 
               aria-invalid="true" aria-required="true" 
               aria-errormessage="vee_Text2">
    </label> 
    <div class="errors--qVgtm">
        <div>Text3</div>
    </div>
</div>

I need to find path to Text3 text element, but exactly via input section:

My way:

//input[@name='newPasswordRepeat']/../../div/div

The path is valid, but it is a long way to go and I want to use the follow-sibling command. But I can't do that

For example, I'm trying to use the parent:: path:

//input[@name='newPasswordRepeat']/parent::
//input[@name='newPasswordRepeat']::parent::
//input[@name='newPasswordRepeat']::parent
//input[@name='newPasswordRepeat']/parent
//input[@name='newPasswordRepeat']/::parent

No one from this order not working, only

//input[@name='newPasswordRepeat']/..

Also I cannot use following-sibling, but in this case another way (.., .) does not exist.

  • How to correctly use XPath locators such as parent, child, following-sibling`?
Valentyn Hruzytskyi
  • 1,772
  • 5
  • 27
  • 59

2 Answers2

0

It's always axis::node_test (compare this answer where I explain various XPath terms).

For example

  • parent::div selects the parent node if it's a <div> (that's the node test).
  • ancestor::div selects all (!) ancestor nodes that are <div>s.
  • following-sibling::div selects all (!) following siblings that are <div>s.

Most of the time there is no guarantee that only a single node is selected. Therefore it's sensible to also have some sort of [predicate] that narrows down the selection to prevent false positives - for example we could verify the @class attribute.

//input[@name='newPasswordRepeat']/parent::label/following-sibling::div[starts-with(@class, 'errors')]/div

Of course parent::label can be shortened to .. if we don't care what kind of element the parent is.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
0

Rather than select a target and have to traverse up to a parent, consider using a predicate on the parent in the first place:

//label[input/@name='newPasswordRepeat']/following-sibling::*[1]/div

will select the div child of the element immediately following the label which contains the targeted input element. No parent:: axis is required.

kjhughes
  • 106,133
  • 27
  • 181
  • 240