1

Ok here the source code and my xpath

I am trying to select 3rd value however it is not working even though selecting first value works

        HtmlDocument hdMyDoc = new HtmlDocument();
        hdMyDoc.LoadHtml(File.ReadAllText("html.txt"));

        HtmlNode hdNodes = hdMyDoc.DocumentNode.SelectSingleNode
        (string.Format("//div[@class='rating'][3]", "div", "class", "rating"));

However when i write [1] it does work

Source code

<div class="anatablo">
    <div class="helpful">
        Bu inceleme sizce faydalı mı?&nbsp; <button class="yesbutton"
        onclick=
        "location.href='http://www.teknobiyotik.com/advancedreviews/helpfulness/post/reviewId/3403/actionName/Yes/';"
        type="button"></button> <button class="nobutton" onclick=
        "location.href='http://www.teknobiyotik.com/advancedreviews/helpfulness/post/reviewId/3403/actionName/No/';"
        type="button"></button>
    </div>

    <div class="baslik">
        Yorumlayan : Ufuk&nbsp;
    </div>

    <table class="alttablo">
        <tbody>
            <tr>
                <td>
                    <table>
                        <tbody>
                            <tr>
                                <td class="yildiz">
                                    <table cellspacing="0" class=
                                    "ratings-list">
                                        <tbody>
                                            <tr>
                                                <td class="label" style=
                                                "font-weight: bold">
                                                Fiyat</td>
                                            </tr>

                                            <tr>
                                                <td>
                                                    <div class="rating-box"
                                                    style=
                                                    "margin-bottom:3px;">
                                                        <div class="rating"
                                                        style=
                                                        "width: 33%;">
                                                        </div>
                                                    </div>
                                                </td>
                                            </tr>

                                            <tr>
                                                <td class="label" style=
                                                "font-weight: bold">
                                                Kullanım Kolaylığı</td>
                                            </tr>

                                            <tr>
                                                <td>
                                                    <div class="rating-box"
                                                    style=
                                                    "margin-bottom:3px;">
                                                        <div class="rating"
                                                        style=
                                                        "width: 66%;">
                                                        </div>
                                                    </div>
                                                </td>
                                            </tr>

                                            <tr>
                                                <td class="label" style=
                                                "font-weight: bold">
                                                Kalite</td>
                                            </tr>

                                            <tr>
                                                <td>
                                                    <div class="rating-box"
                                                    style=
                                                    "margin-bottom:3px;">
                                                        <div class="rating"
                                                        style=
                                                        "width: 99%;">
                                                        </div>
                                                    </div>
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </td>

                                <td class="yorumalani">
                                    <div class="yorumbaslik">
                                        <strong>Kocaman birşey</strong> (
                                        06.07.2013 )
                                    </div>Bu fiyata akasa ürünü hem de
                                    isteyebileceğinizden daha büyük bir
                                    mouse alanı. Yere adeta yapışıyor.
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
        </tbody>
    </table>

    <table width="100%">
        <tbody>
            <tr>
                <td>
                    <div class="review-footer" style="display:block;">
                        <table style="width:100%;">
                            <tbody>
                                <tr>
                                    <td class="socialshare">
                                        <!-- AW_AdvancedReviews Socialshare block start -->
                                        Bu incelemeyi paylaşın <a href=
                                        "http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.teknobiyotik.com%2Freview%2Fproduct%2Fview%2Fid%2F3403%2F%3F___SID%3DU&amp;t=Kocaman%20bir%C5%9Fey"
                                        target="_blank"><img alt="Facebook"
                                        height="16" src=
                                        "http://www.teknobiyotik.com/skin/frontend/default/default/advancedreviews/images/link-facebook.gif"
                                        title="Add to Facebook" width=
                                        "16"></a> 
                                        <!-- AW_AdvancedReviews Socialshare block end -->
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
</div>

<ul></ul>

Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
  • I don't understand how your `string.Format("//div[@class='rating'][3]", "div", "class", "rating")` works. Maybe you forgot about curly braces? Maybe you thought about `string.Format("//div[@class='rating'][{2}]", "div", "class", "rating")`. Remember that string.format uses 0-based place holders. – rraszewski Nov 13 '14 at 21:56
  • @rraszewski //div[@class='rating'][1]" or //div[@class='rating']" works fine – Furkan Gözükara Nov 13 '14 at 21:58
  • I still don't understand why you use `string.format` with arguments `"div", "class", "rating"` and you don't have any placeholders. – rraszewski Nov 13 '14 at 22:28
  • @rraszewski ignore it :) it has no effect – Furkan Gözükara Nov 13 '14 at 22:34

1 Answers1

2

Try with

hdMyDoc.DocumentNode.SelectSingleNode("(//div[@class='rating'])[3]");

The ( and ) brackets in xpath are crucial.

rraszewski
  • 1,135
  • 7
  • 21
  • ty now it works. can you explain in details what difference () makes ? i never used it until now and everytime worked – Furkan Gözükara Nov 13 '14 at 23:17
  • I thing that `hdMyDoc.DocumentNode.SelectSingleNode("//div[@class='rating'][1]");` doesn't work at all, because if you change 'SelectSingleNode` to `SelectNodes` then you always get 3 elements. So `[1]` is just omitted. I've tested few solutions and of course browsed the net. Look there http://msdn.microsoft.com/en-us/library/ms256122(v=vs.110).aspx and there http://stackoverflow.com/questions/4007413/xpath-query-to-get-nth-instance-of-an-element – rraszewski Nov 14 '14 at 06:43