I found 10 elements using:
divs_reviews = self.driver.find_elements(By.XPATH, '//div[@class="c-review-block"]')
Then i’m trying get information from collected divs
by xpath in cycle:
for div_review in divs_reviews: date = div_review.find_element(By.XPATH, '//div[@class="c-review-block__row"]/span[@class="c-review-block__date"]').text.replace('Время отзыва: ', '') review_title = div_review.find_element(By.XPATH, '//div[@class="c-review-block__row"]//h3[@lang="ru"]')
But always i get first element
Question: Is it possible to get information from each block in cycle using relative xpath with already founded element by selenium?
One of divs HTML:
<div class="c-review-block" itemprop="review" itemscope="" itemtype="http://schema.org/Review" bis_skin_checked="1"> <div itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization" bis_skin_checked="1"> <meta itemprop="name" content="Booking.com B.V."> </div> <div class="bui-grid" bis_skin_checked="1"> <div class="bui-grid__column-3 c-review-block__left" bis_skin_checked="1"> <div class="c-review-block__row c-review-block__guest" bis_skin_checked="1"> <div class="c-guest bui-avatar-block" bis_skin_checked="1"><div class="c-avatar bui-avatar " bis_skin_checked="1"><img class="bui-avatar__image" src="https://cf.bstatic.com/static/img/review/avatars/ava-y/ea90dc6a44b984e60291f9db43e96ddfa72b1aef.png" data-component="image-fallback" data-fallback-src="https://cf.bstatic.com/static/img/review/avatars/ava-y/ea90dc6a44b984e60291f9db43e96ddfa72b1aef.png" alt="" aria-hidden="true"></div> <div class="bui-avatar-block__text" bis_skin_checked="1"> <span class="bui-avatar-block__title">Yulia</span><span class="bui-avatar-block__subtitle"><span class="bui-flag bui-avatar-block__flag"> <img src="https://cf.bstatic.com/static/img/flags/16/ru/ca11fa7209330bf72a7130609a4ce89ec4494563.png" alt=""> </span>Россия</span></div> </div> </div> <div class="c-review-block__row c-review-block__room-info-row review-block__room-info--disabled" data-room-id="28826701" data-component="ugcs/shared/room-info" bis_skin_checked="1"> <ul class="bui-list bui-list--text bui-list--icon bui_font_caption"> <li class="bui-list__item review-block__room-info--disabled" data-room-id="28826701" data-component="ugcs/shared/room-info"> <a href="#" class="c-review-block__room-link"> <span class="bui-list__icon" role="presentation"> <svg class="bk-icon -streamline-bed_double" height="16" width="16" viewBox="0 0 24 24" role="presentation" aria-hidden="true" focusable="false"><path d="M3.75 11.25V9a.75.75 0 0 1 .75-.75h6a.75.75 0 0 1 .75.75v2.25a.75.75 0 0 0 1.5 0V9a2.25 2.25 0 0 0-2.25-2.25h-6A2.25 2.25 0 0 0 2.25 9v2.25a.75.75 0 0 0 1.5 0zm9 0V9a.75.75 0 0 1 .75-.75h6a.75.75 0 0 1 .75.75v2.25a.75.75 0 0 0 1.5 0V9a2.25 2.25 0 0 0-2.25-2.25h-6A2.25 2.25 0 0 0 11.25 9v2.25a.75.75 0 0 0 1.5 0zm-10 .75h18.5c.69 0 1.25.56 1.25 1.25V18l.75-.75H.75l.75.75v-4.75c0-.69.56-1.25 1.25-1.25zm0-1.5A2.75 2.75 0 0 0 0 13.25V18c0 .414.336.75.75.75h22.5A.75.75 0 0 0 24 18v-4.75a2.75 2.75 0 0 0-2.75-2.75H2.75zM0 18v3a.75.75 0 0 0 1.5 0v-3A.75.75 0 0 0 0 18zm22.5 0v3a.75.75 0 0 0 1.5 0v-3a.75.75 0 0 0-1.5 0zm-.75-6.75V4.5a2.25 2.25 0 0 0-2.25-2.25h-15A2.25 2.25 0 0 0 2.25 4.5v6.75a.75.75 0 0 0 1.5 0V4.5a.75.75 0 0 1 .75-.75h15a.75.75 0 0 1 .75.75v6.75a.75.75 0 0 0 1.5 0z"></path></svg> </span> <span></span> <div class="bui-list__body" bis_skin_checked="1"> Классический двухместный номер с 1 кроватью или 2 отдельными кроватями </div> </a> </li> </ul> </div> <ul class="bui-list bui-list--text bui-list--icon bui_font_caption c-review-block__row c-review-block__stay-date"> <li class="bui-list__item"> <span class="bui-list__icon" role="presentation"> <svg class="bk-icon -streamline-calendar" height="16" width="16" viewBox="0 0 24 24" role="presentation" aria-hidden="true" focusable="false"><path d="M22.502 13.5v8.25a.75.75 0 0 1-.75.75h-19.5a.75.75 0 0 1-.75-.75V5.25a.75.75 0 0 1 .75-.75h19.5a.75.75 0 0 1 .75.75v8.25zm1.5 0V5.25A2.25 2.25 0 0 0 21.752 3h-19.5a2.25 2.25 0 0 0-2.25 2.25v16.5A2.25 2.25 0 0 0 2.252 24h19.5a2.25 2.25 0 0 0 2.25-2.25V13.5zm-23.25-3h22.5a.75.75 0 0 0 0-1.5H.752a.75.75 0 0 0 0 1.5zM7.502 6V.75a.75.75 0 0 0-1.5 0V6a.75.75 0 0 0 1.5 0zm10.5 0V.75a.75.75 0 0 0-1.5 0V6a.75.75 0 0 0 1.5 0z"></path></svg> </span> <div class="bui-list__body" bis_skin_checked="1"> 4 ночи · <span class="c-review-block__date"> Сентябрь 2019 </span> </div> </li> </ul> <ul class="bui-list bui-list--text bui-list--icon bui_font_caption review-panel-wide__traveller_type c-review-block__row"> <li class="bui-list__item"> <span class="bui-list__icon" role="presentation"> <svg class="bk-icon -streamline-couple" height="16" width="16" viewBox="0 0 24 24" role="presentation" aria-hidden="true" focusable="false"><path d="M8.25 3.75a2.25 2.25 0 1 1-4.5 0 2.25 2.25 0 0 1 4.5 0zm1.5 0a3.75 3.75 0 1 0-7.5 0 3.75 3.75 0 0 0 7.5 0zM12 13.5a6 6 0 0 0-12 0v2.25c0 .414.336.75.75.75H3l-.746-.675.75 7.5A.75.75 0 0 0 3.75 24h4.5a.75.75 0 0 0 .746-.675l.75-7.5L9 16.5h2.25a.75.75 0 0 0 .75-.75V13.5zm-1.5 0v2.25l.75-.75H9a.75.75 0 0 0-.746.675l-.75 7.5.746-.675h-4.5l.746.675-.75-7.5A.75.75 0 0 0 3 15H.75l.75.75V13.5a4.5 4.5 0 1 1 9 0zm9.75-9.75a2.25 2.25 0 1 1-4.5 0 2.25 2.25 0 0 1 4.5 0zm1.5 0a3.75 3.75 0 1 0-7.5 0 3.75 3.75 0 0 0 7.5 0zM13.5 16.5H15l-.746-.675.75 7.5a.75.75 0 0 0 .746.675h4.5a.75.75 0 0 0 .746-.675l.75-7.5L21 16.5h2.25a.75.75 0 0 0 .75-.75V13.5a6 6 0 0 0-11.143-3.086.75.75 0 0 0 1.286.772 4.5 4.5 0 0 1 8.357 2.315v2.249l.75-.75H21a.75.75 0 0 0-.746.675l-.75 7.5.746-.675h-4.5l.746.675-.75-7.5A.75.75 0 0 0 15 15h-1.5a.75.75 0 0 0 0 1.5z"></path></svg> </span> <div class="bui-list__body" bis_skin_checked="1"> Пара </div> </li> </ul> </div> <div class="bui-grid__column-9 c-review-block__right" bis_skin_checked="1"> <div class="c-review-block__row" bis_skin_checked="1"> <span class="c-review-block__badge"> <span class="c-review-block__badge__icon"> <svg class="bk-icon -iconset-star" fill="#FFFFFF" height="10" width="10" viewBox="0 0 128 128" role="presentation" aria-hidden="true" focusable="false"><path d="M117.4 56.3L89.6 77.5a1.7 1.7 0 0 0-.3 1.2l10.7 34.2c1.2 3.9-.8 5.7-1.6 6.3-.7.5-3.2 1.8-6.4-.6L64.3 97.4a1 1 0 0 0-.6 0l-27.8 21.2a6 6 0 0 1-3.6 1.4 4.7 4.7 0 0 1-2.7-.8c-.8-.6-2.8-2.4-1.6-6.3l10.6-34.2a1.7 1.7 0 0 0-.3-1.2L10.4 56.3c-3.1-2.4-2.5-5.1-2.3-6S9.6 47 13.5 47h34.3a1.3 1.3 0 0 0 .7-.6L59 12.2C60.2 8.4 62.9 8 63.9 8s3.7.4 4.9 4.2l10.6 34.3a1.3 1.3 0 0 0 .7.6h34.2c4 0 5.1 2.5 5.4 3.3s.9 3.5-2.3 6z"></path></svg> </span> Популярный отзыв </span> <span class="c-review-block__date"> Время отзыва: 29 сентября 2019 </span> <div class="bui-grid" bis_skin_checked="1"> <div class="bui-grid__column-10" bis_skin_checked="1"> <h3 lang="ru" class=" c-review-block__title c-review__title--ltr "> The perfect Hotel Santo Stefano - it’s smile of Venice, the heart of Italiano!!! Belissimo! ✨✨✨✨ </h3> </div> <div class="bui-grid__column-2 bui-u-text-right" bis_skin_checked="1"> <div class="bui-review-score c-score" bis_skin_checked="1"> <div class="bui-review-score__badge" aria-label="Оценка " bis_skin_checked="1"> 10 </div> </div> </div> </div> </div> <div class="c-review-block__row" bis_skin_checked="1"> <div class="c-review" bis_skin_checked="1"> <div class="c-review__row" bis_skin_checked="1"> <p class="c-review__inner c-review__inner--ltr"> <span class="c-review__prefix c-review__prefix--color-green"><svg class="bk-icon -iconset-review_great c-review__icon" height="128" width="128" viewBox="0 0 128 128" role="presentation" aria-hidden="true" focusable="false"><path d="M64 8a56 56 0 1 0 56 56A56 56 0 0 0 64 8zm0 104a48 48 0 1 1 48-48 48 48 0 0 1-48 48zM44 64a8 8 0 1 1 8-8 8 8 0 0 1-8 8zm48-8a8 8 0 1 1-8-8 8 8 0 0 1 8 8zm-4.8 21.6a4 4 0 0 1 .6 3.6A24.3 24.3 0 0 1 64 97c-9.7 0-15.7-4.2-19-7.8a22.7 22.7 0 0 1-4.8-8A4 4 0 0 1 44 76h40a4 4 0 0 1 3.2 1.6z"></path></svg><span class="bui-u-sr-only">Понравилось</span> </span><span aria-hidden="true"> · </span><span class="c-review__body" lang="ru">Локация перфекто!</span> </p> </div> <div class="c-review__row lalala" bis_skin_checked="1"> <p lang="ru" class="c-review__inner c-review__inner--ltr"> <span class="c-review__prefix"><svg class="bk-icon -iconset-review_poor c-review__icon" height="128" width="128" viewBox="0 0 128 128" role="presentation" aria-hidden="true" focusable="false"><path d="M64 8a56 56 0 1 0 56 56A56 56 0 0 0 64 8zm0 104a48 48 0 1 1 48-48 48 48 0 0 1-48 48zM44 64a8 8 0 1 1 8-8 8 8 0 0 1-8 8zm48-8a8 8 0 1 1-8-8 8 8 0 0 1 8 8zm-5.2 30.2a4 4 0 1 1-5.6 5.6c-10.5-10.4-24-10.4-34.4 0a4 4 0 0 1-5.6-5.6c13.6-13.7 32-13.7 45.6 0z"></path></svg><span class="bui-u-sr-only">Не понравилось</span> </span><span aria-hidden="true"> · </span><span class="c-review__body" lang="ru">Таких пунктов нет</span> </p> </div> </div> </div> <div class="c-review-block__row" bis_skin_checked="1"> <ul class="c-review-block__photos " data-photos-group="c67fd72dc494a633" data-component="ugcs/review-block/photos"> <li class="c-review-block__photos__item" style=""> <button class="c-review-block__photos__button" data-photos-index="1" data-photos-src="https://cf.bstatic.com/xdata/images/xphoto/max1280x900/78907349.jpg?k=727ce830effaeb3e1976a8099d6c0bc4e5c4c4aa6e93b502858f3d98f4f580ec&o=" data-photos-large-src="https://cf.bstatic.com/xdata/images/xphoto/max1024x768/78907349.jpg?k=727ce830effaeb3e1976a8099d6c0bc4e5c4c4aa6e93b502858f3d98f4f580ec&o=" style="background-image: url(https://cf.bstatic.com/xdata/images/xphoto/square80/78907349.jpg?k=727ce830effaeb3e1976a8099d6c0bc4e5c4c4aa6e93b502858f3d98f4f580ec&o=);background-image: -webkit-image-set(url(https://cf.bstatic.com/xdata/images/xphoto/square80/78907349.jpg?k=727ce830effaeb3e1976a8099d6c0bc4e5c4c4aa6e93b502858f3d98f4f580ec&o=) 1x, url(https://cf.bstatic.com/xdata/images/xphoto/square160/78907349.jpg?k=727ce830effaeb3e1976a8099d6c0bc4e5c4c4aa6e93b502858f3d98f4f580ec&o=) 2x)"> </button> </li> </ul> </div> <svg class="bk-icon -iconset-warning" height="128" style="display:none;" width="128" viewBox="0 0 128 128" role="presentation" aria-hidden="true" focusable="false"><path d="M64 8a56 56 0 1 0 56 56A56 56 0 0 0 64 8zm0 91a8 8 0 1 1 8-8 8 8 0 0 1-8 8zm8-61l-5 39h-6l-5-39a8 8 0 1 1 16 0z"></path></svg> <svg class="bk-icon -iconset-navarrow_right" height="128" style="display:none;" width="128" viewBox="0 0 128 128" role="presentation" aria-hidden="true" focusable="false"><path d="M54.3 96a4 4 0 0 1-2.8-6.8L76.7 64 51.5 38.8a4 4 0 0 1 5.7-5.6L88 64 57.2 94.8a4 4 0 0 1-2.9 1.2z"></path></svg> <svg class="bk-icon -iconset-navarrow_left" height="128" style="display:none;" width="128" viewBox="0 0 128 128" role="presentation" aria-hidden="true" focusable="false"><path d="M73.7 96a4 4 0 0 1-2.9-1.2L40 64l30.8-30.8a4 4 0 0 1 5.7 5.6L51.3 64l25.2 25.2a4 4 0 0 1-2.8 6.8z"></path></svg> <svg class="bk-icon -iconset-close" height="128" style="display:none;" width="128" viewBox="0 0 128 128" role="presentation" aria-hidden="true" focusable="false"><path d="M69.7 64l33.1-33.2a4 4 0 0 0-5.6-5.6L64 58.3 30.8 25.2a4 4 0 1 0-5.6 5.6L58.3 64 25.2 97.2a4 4 0 1 0 5.6 5.6L64 69.7l33.2 33.1a4 4 0 0 0 5.6-5.6z"></path></svg> <div class="c-review-block__row c-review-block__row--helpful-vote bui-u-text-right" bis_skin_checked="1"> <div class="c-review-block__actions" bis_skin_checked="1"> <div class="review-helpful__container" data-component="reviews/review-helpful-unhelpful" bis_skin_checked="1"> <p class="review-helpful__vote-feedback-message review-helpful-heart-vote-feedback-message review-helpful__vote-others-helpful " style="display: none;"> </p> <p class="review-helpful__vote-feedback-message review-helpful-heart-vote-feedback-message --unhelpful"> </p> <form class="review-helpful__form clearfix" action="/vote_review" method="post" data-current-count="0"> <input type="hidden" name="hotel_id" value="288267"> <input type="hidden" name="review_url" value="c67fd72dc494a633"> <input type="hidden" name="review_id" value="0"> <input type="hidden" name="vote_type" value="1"> <input type="hidden" name="vote_value" value="1"> <button type="submit" class="review-helpful__form-submit review-helpful-heart-submit" data-type="helpful"> <svg class="bk-icon -iconset-thumbs_up" fill="#0071C2" height="14" width="14" viewBox="0 0 128 128" role="presentation" aria-hidden="true" focusable="false"><path d="M8 76a12 12 0 0 1 3.6-8.6A12 12 0 0 1 21 48h35.9L49 28a15.2 15.2 0 0 1-1-4 8 8 0 0 1 15-4l17 36v56H30a12 12 0 0 1-12-10.7 12 12 0 0 1-6.6-17A12 12 0 0 1 8 76zm112-20H88v56h32z"></path></svg> Полезно </button> </form> <form class="review-helpful__form --unhelpful clearfix" action="/vote_review" method="post"> <input type="hidden" name="hotel_id" value="288267"> <input type="hidden" name="review_url" value="c67fd72dc494a633"> <input type="hidden" name="review_id" value="0"> <input type="hidden" name="vote_type" value="4"> <input type="hidden" name="vote_value" value="1"> <button type="submit" class="review-helpful__form-submit review-helpful-heart-submit" data-type="unhelpful"> <svg class="bk-icon -iconset-thumbs_down" fill="#0071C2" height="14" width="14" viewBox="0 0 128 128" role="presentation" aria-hidden="true" focusable="false"><path d="M11.4 43.6a12 12 0 0 1 6.7-17A12 12 0 0 1 30 16h50v56l-17 36a8 8 0 0 1-15-4 15.2 15.2 0 0 1 1-4l7.9-20H21a12 12 0 0 1-9.4-19.4 12 12 0 0 1-.2-17zM120 16H88v56h32z"></path></svg> Бесполезно </button> </form> </div> <div bis_skin_checked="1"> </div> </div> </div> </div> </div> </div>
Advertisement
Answer
Yes, you can easily find each block data using relative xpath .So you have to make relative xpath using .//
divs_reviews = self.driver.find_elements(By.XPATH, '//div[@class="c-review-block"]') for div_review in divs_reviews: date = div_review.find_element(By.XPATH, './/div[@class="c-review-block__row"]/span[@class="c-review-block__date"]').text.replace('Время отзыва: ', '') review_title = div_review.find_element(By.XPATH, './/div[@class="c-review-block__row"]//h3[@lang="ru"]')