Search for text and select parent element? (Ruby)

I would like to find the text inside html, then select the parent of the element so that I can use it to store the link identifier as a string. (I use watir for this, since the purpose of this is to automatically test)

so here is an example:

<html> <body> <div id="container"> <span class="story"> <span> ref4040 </span> </span> <div id="text"> example </div> </div> </body> </html> 

Is there a way that I can use ruby ​​to search for the text "example" and then select the parent so that I can store the reference identifier within the range as a string?

(I know that you can do this in a simple way in this example of selecting a div, then span, but in a project I'm actually working on, this is not possible. The only possible way to do this is to find the text then select the link.)

+4
source share
1 answer

You can get the parent by doing .parent .

So you can do something like:

 browser.div(:text, 'example').parent.span(:class, 'story').text 
+8
source

Source: https://habr.com/ru/post/1433406/


All Articles