How to define an xpath expression that only extracts portable items from the first of two similar divs?

The below HTML block displays divs.

//div[contains(@class,'top-container')]//font/text()

I use the xpath expression above to try to get any data in the first div below, in which a hyphen is used to delimit the data:

Wednesday - Chess at Higgins Stadium
Thursday - Cook-off

The problem is that I get data from the second div below, for example:

Monday 10:00 - 11:00
Tuesday 10:00 - 11:00

How to get data only from the first div? (I also want to exclude any elements in the first div that do not contain this hyphen data)?

<div class="top-container"> 
<div dir="ltr"> 
<div dir="ltr"><font face="Arial" color="#000000" size="2">Wednesday - Chess at Higgins Stadium</font></div> 
<div dir="ltr"><font face="Arial" size="2">Thursday - Cook-off</font></div> 
<div dir="ltr"><font face="Arial" size="2"></font>&nbsp;</div> 
<div dir="ltr">&nbsp;</div> 
<div dir="ltr"><font face="Arial" color="#000000" size="2"></font>&nbsp;</div>
</div> 

<div dir="ltr"> 
<div RE><font face="Arial"> 
<div dir="ltr"> 
<div RE><font face="Arial" size="2"><strong>Alex Dawkin </strong></font></div> 
<div RE><font face="Arial" size="2">Monday 10:00 - 11:00 </font></div> 
<div RE><font size="2">Tuesday 10:00 - 11:00 </font></div> 
<div RE> 
<div RE><font face="Arial" size="2"></font></div><font face="Arial" size="2"></font></div> 
<div RE>&nbsp;</div> 
<div RE>&nbsp;</div> 
+3
source share
1 answer

Your XPATH matched the any element font, which is a descendant <div class="top-container">.

div[1] div "top-container". XPATH, .

//div[contains(concat(' ',@class,' '),' top-container '))]/div[1]//font/text()

, text(), "-", , text().

//div[contains(concat(' ',@class,' '),' top-container '))]/div[1]//font/text()[contains(.,'-')]

"-", ?

text() node , text() . node , node .

, text(), , whitespace, :

//div[contains(concat(' ',@class,' '),' top-container '))]/div[1]//font/text()[normalize-space()]

normalize-space() . , text() ( &nbsp;), false() , text(), - , .

+1

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


All Articles