Select text from inner element

yiannisyiannis Posts: 8

I want to check text from an element that is inside another element.

So I want to verify that text form div class="alert" is the correct one. But selecting this div from the validation tool does not work.

My first though was to extract the text using javascript. So

  • Element locator = Use script
  • Language = Javascript (legacy)
  • text = return document.querySelector("div.alert div span").textContent.trim();

and of course this didn't work. After some searching I found the below comment on how to create a javascript script.

Do we really need to include all this code, imports etc. in order to get a simple element?


  • jakubiakjakubiak Posts: 696 admin

    From that dialog you do not have the ability to select "text", since there are no text nodes that are a direct child of that div element. So in the dialog choose a different property (even though you don't want it), and finish the dialog. Then, in the Browser Validation Tool "Validations" tab, go to the validation that was just added, and type "text" in the "Element Property" field and update the Expected Value field to the value that you expect. This will cause it to get all text nodes that are direct or indirect children of the div node, and will allow you to do the assertion that you want to do.

Sign In or Register to comment.