Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for text then selecting the parent element? (ruby)

Tags:

ruby

watir

I'd like to search for text within the html then select the parent of the element so I can use that to store a reference ID as a string. (I'm using watir to do this as the purpose for it is automated testing)

so here's 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 I can use ruby to search for the text "example" then select the parent so I can store the reference ID within the span as a string?

(I know you can do it the simple way in this example of selecting the div then the span, but on the project I am actually working on, this is not possible. The only possible way of doing it is via search for the text then selecting the reference.)

like image 775
samayres1992 Avatar asked Feb 19 '23 04:02

samayres1992


1 Answers

You can get the parent element by doing .parent.

So you could do something like:

browser.div(:text, 'example').parent.span(:class, 'story').text
like image 53
Justin Ko Avatar answered Mar 15 '23 03:03

Justin Ko