Home > front end >  xpath inside founded elements using selenium
xpath inside founded elements using selenium

Time:08-24

I found 10 elements using:

 divs_reviews = self.driver.find_elements(By.XPATH, '//div[@]')

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[@]/span[@]').text.replace('Время отзыва: ', '')
    review_title = div_review.find_element(By.XPATH, '//div[@]//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  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  bis_skin_checked="1">
<div  bis_skin_checked="1">
<div  bis_skin_checked="1">
<div  bis_skin_checked="1"><div  bis_skin_checked="1"><img  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  bis_skin_checked="1">
                <span >Yulia</span><span ><span >
                                    <img src="https://cf.bstatic.com/static/img/flags/16/ru/ca11fa7209330bf72a7130609a4ce89ec4494563.png" alt="">
                                </span>Россия</span></div>
        </div>
</div>
<div  data-room-id="28826701" data-component="ugcs/shared/room-info" bis_skin_checked="1">
<ul >
<li  data-room-id="28826701" data-component="ugcs/shared/room-info">
<a href="#" >
<span  role="presentation">
<svg  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  bis_skin_checked="1">
Классический двухместный номер с 1 кроватью или 2 отдельными кроватями
</div>
</a>
</li>
</ul>
</div>
<ul >
<li >
<span  role="presentation">
<svg  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  bis_skin_checked="1">
4 ночи ·&nbsp;
<span >
Сентябрь 2019
</span>
</div>
</li>
</ul>
<ul >
<li >
<span  role="presentation">
<svg  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  bis_skin_checked="1">
Пара
</div>
</li>
</ul>
</div>
<div  bis_skin_checked="1">
<div  bis_skin_checked="1">
<span >
<span >
<svg  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 >
Время отзыва: 29 сентября 2019
</span>
<div  bis_skin_checked="1">
<div  bis_skin_checked="1">
<h3 lang="ru" >
The perfect Hotel Santo Stefano - it’s smile of Venice, the heart of Italiano!!! Belissimo! ✨✨✨✨
</h3>
</div>
<div  bis_skin_checked="1">
 <div  bis_skin_checked="1"> <div  aria-label="Оценка " bis_skin_checked="1"> 10 </div> </div>  
</div>
</div>
</div>
<div  bis_skin_checked="1">
<div  bis_skin_checked="1">
<div  bis_skin_checked="1">
<p >
<span ><svg  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 >Понравилось</span> </span><span aria-hidden="true">&nbsp;·&nbsp;</span><span  lang="ru">Локация перфекто!</span>
</p>
</div>
<div  bis_skin_checked="1">
<p lang="ru" >
<span ><svg  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 >Не понравилось</span> </span><span aria-hidden="true">&nbsp;·&nbsp;</span><span  lang="ru">Таких пунктов нет</span>
</p>
</div>
</div>
</div>
<div  bis_skin_checked="1">
<ul  data-photos-group="c67fd72dc494a633" data-component="ugcs/review-block/photos">
<li  style="">
<button  data-photos-index="1" data-photos-src="https://cf.bstatic.com/xdata/images/xphoto/max1280x900/78907349.jpg?k=727ce830effaeb3e1976a8099d6c0bc4e5c4c4aa6e93b502858f3d98f4f580ec&amp;o=" data-photos-large-src="https://cf.bstatic.com/xdata/images/xphoto/max1024x768/78907349.jpg?k=727ce830effaeb3e1976a8099d6c0bc4e5c4c4aa6e93b502858f3d98f4f580ec&amp;o=" style="background-image: url(https://cf.bstatic.com/xdata/images/xphoto/square80/78907349.jpg?k=727ce830effaeb3e1976a8099d6c0bc4e5c4c4aa6e93b502858f3d98f4f580ec&amp;o=);background-image: -webkit-image-set(url(https://cf.bstatic.com/xdata/images/xphoto/square80/78907349.jpg?k=727ce830effaeb3e1976a8099d6c0bc4e5c4c4aa6e93b502858f3d98f4f580ec&amp;o=) 1x, url(https://cf.bstatic.com/xdata/images/xphoto/square160/78907349.jpg?k=727ce830effaeb3e1976a8099d6c0bc4e5c4c4aa6e93b502858f3d98f4f580ec&amp;o=) 2x)">
</button>
</li>
</ul>
</div>
<svg  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  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  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  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  bis_skin_checked="1">
<div  bis_skin_checked="1">
<div  data-component="reviews/review-helpful-unhelpful" bis_skin_checked="1">
<p  style="display: none;">
</p>
<p >
</p>
<form  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"  data-type="helpful">
<svg  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  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"  data-type="unhelpful">
<svg  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>

CodePudding user response:

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[@]')
for div_review in divs_reviews:
    date = div_review.find_element(By.XPATH, './/div[@]/span[@]').text.replace('Время отзыва: ', '')
    review_title = div_review.find_element(By.XPATH, './/div[@]//h3[@lang="ru"]')

CodePudding user response:

You are almost there, the only 1 thing you are missing is a dot . at the front of XPath expression.
It should be

date = div_review.find_element(By.XPATH, './/div[@]/span[@]').text.replace('Время отзыва: ', '')

And

review_title = div_review.find_element(By.XPATH, './/div[@]//h3[@lang="ru"]')

When using '//div[@]/span[@]' XPath expression it will search for the matching locator from the top of the page until it finds the first match.
While when we have that dot . it means to start searching inside the current node i.e. inside the parent element div_review
So, your entire code can be:

for div_review in divs_reviews:
    date = div_review.find_element(By.XPATH, './/div[@]/span[@]').text.replace('Время отзыва: ', '')
    review_title = div_review.find_element(By.XPATH, './/div[@]//h3[@lang="ru"]')
  • Related