Get multiple results xpath div text and next div text

636 views Asked by At

Using xpath, can I get 2 results out of a page at a time? For example, using xpath I can get the first img element:

xpath='//div[@class="forecast-element graphic-box"]/img

...and the next sibling class="forecast-element".

I tried "and" without success:

xpath='//div[@class="forecast-element graphic-box"]/img and //div[@class="forecast-element"]'

also:

xpath='//div[@class="forecast-element graphic-box"]/img and following-sibling:://div[@class="forecast-element graphic-box"]'

I have this html:

<div class='forecast-element graphic-box ' style="background-image:url('/assets/images/forecast/BluePattern.png');">
    <h4 style="color: #FFF;">AVALANCHE DANGER <span style="margin-left: 60px;"> MORNING </span><span style="margin-left: 210px;"> AFTERNOON</span></h4>
    <img src="/images/forecast/2014-11-23_teton_hazard.gif" alt="Teton Area avalanche hazard rating for 11/23/2014" />
    <div style='margin: 2px auto;'><a href="/assets/pdfs/North%20American%20Danger%20Scale.pdf" style='font-size: 14px; color: #CCC;'>View full danger scale definitions</a>
    </div>
    <a href="/assets/pdfs/North%20American%20Danger%20Scale.pdf" style='font-size: 14px;'>
        <img src="/assets/images/forecast/DangerScale.png" style="margin-left: 150px; margin-top: 5px;" alt='Avalanche danger scale ratings'>
    </a>
</div>

<div class='forecast-element'>
    <h3 style='margin-bottom: 8px;'>GENERAL AVALANCHE ADVISORY</h3>
    Moderate to heavy snowfall combined with strong southwesterly to northwesterly ridgetop winds have created unstable avalanche conditions. New wind slabs have developed at the mid and upper elevations. Snowfall over the past 24 hours has also added weight to existing weak layers near the base of the snowpack. Early season snowfall can easily cloud people’s judgment. Cautious route finding and conservative decision making will be essential for safe travel in avalanche terrain today.</div>

I would like to use following-sibling, as the item is right after the graphic-box element and there are other forecast-elements in the html above and below. BTW, I am using YQL if that makes a difference...

Ideally the results would be (psuedo here):

xpath imgsrc = "/images/forecast/2014-11-23_teton_hazard.gif"

xpath text = "GENERAL AVALANCHE ADVISORY Moderate to heavy snowfall combined with strong southwesterly to northwesterly........"

Thanks!

1

There are 1 answers

2
caesay On BEST ANSWER

The correct syntax for selecting multiple nodes is |

Try this:

//div[@class="forecast-element graphic-box"]/img | //div[@class="forecast-element"]

As you mentioned, these are two separate query elements. In order to select following elements simply do this

//div[@class="forecast-element graphic-box"]/img | //div[@class="forecast-element graphic-box"]/following-sibling::div[@class="forecast-element"]

In some xpath parsers, this will also work:

//div[@class="forecast-element graphic-box"]/(img|following-sibling::div[@class="forecast-element"])