Furkan Gözükara
Furkan Gözükara

Reputation: 23800

Why this Xpath htmlagilitypack select not working

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>

Upvotes: 1

Views: 738

Answers (1)

rraszewski
rraszewski

Reputation: 1145

Try with

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

The ( and ) brackets in xpath are crucial.

Upvotes: 2

Related Questions