Accessing a div element in an array of li elements

I am trying to access a div in a li array

<ul> <li class="views-row views-row-1 views-row-odd views-row-first"> <div class="news-item"> </li> <li class="views-row views-row-2 views-row-even"> <li class="views-row views-row-3 views-row-odd"> <div class="news-item"> <div class="image"> <div class="details with-image"> <h2> <p class="standfirst">The best two-seat </p> <div class="meta"> <div class="pub-date">26 April 2012</div> <div class="topic-bar clearfix"> <div class="topic car_review">review</div> </div> </div> </div> </div> </li> 

I am trying to access the "div class =" topic car_review "> car overview" and get its text. The reason I specifically use this text is because, depending on what the text is, it introduces specific steps.

The code I'm using is

 @topic = @browser.li(:class => /views-row-#{x}/).div(:class,'news-item').div(:class,'details').div(:class,'meta').div(:class,/topic /).text 

the script worked fine and suddenly stopped working and just couldn't get a div (: class, 'news-item').

The error message I get is

 unable to locate element, using {:class=>"news-item", :tag_name=>"div"} (Watir::Exception::UnknownObjectException) 

I tried div (: class => / news- /), but still it just couldn't find this element

I'm really stuck !!!

-1
source share
1 answer

I assume that when you do li(:class => /views-row-#{x}/) , does x mean that you iterate over all the rows? If so, then your script will exit with a row-2 error, since it does not contain a news-item div (an error appears as a result).

If there is only one of these div tags in the car_review tag, you can simply do:

 @topic = @browser.div(:class, 'topic car_review') 

Update - Iterate over each LI:

If you need to iterate over each LI, you can do the following:

 @browser.lis.each do |li| @topic = li.div(:class, 'topic car_review').text end 
+1
source

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


All Articles