Get a sibling website element using a css selector

How can I get a link to button

? which is always the sibling of the text I'm looking for.

In the transportimeter test:

let spanText = 'My text';
let spanRef = element(by.css('span[name=' + spanText + ']'));

      

Html:

<div>
  <span name="My text">My text</span>
  <button type="button">
    Click me!
  </button>
</div>

      

+3


source to share


2 answers


You can use the adjacent selector (+) :

by.css('span[name=' + spanText + '] + button')

      

Note that you can add quotation marks to the environment spanText

as it contains a space. See valid IDs



With quotes and using the literal pattern :

by.css(`span[name="${spanText}"] + button`)

      

+5


source


Perhaps it will help you:

Xpath :

  • // span [@ name = "My text"] / next-sibling :: button
  • // span [contains (text (), 'My text')] / next-sibling :: button
  • // div [span [@ name = "My text"]] / button


CSS

  • span [name = "My text"] + button
0


source







All Articles