I'm trying to scrape some text of a website that has a list of products. What is the XPath to get the text of only the first occurrence of a class tag in each div? In the code below, I need the first occurence of the text of span "bar" for each div "foo".
So I need the XPath that gives me only "Year A", "Year C", etc.
I'm new with this and have no clue to do this. Many thanks for any help offered!
<div class="foo">
<span class="bar">year A</span>
<span class="qux">some text</span>
<span class="bar">year B</span>
</div>
<div class="foo">
<span class="bar">year C</span>
<span class="qux">some text</span>
<span class="bar">year D</span>
</div>
Etc.
With something like //span[@class='bar'][1]/text() one would only get "Year A".
With something like //*[contains(@class, 'bar')]/text() one would get "Year A", "Year B", "Year C" and "Year D".
I'm scraping multiple pages and the number of items on each page is different. The class name "bar" is only used for the elements I need, so the problem described here: What is the XPath expression to find only the first occurrence? does not apply.