How can we calculate length of the pagination

The linked image is the page and it shows there are 5+ pages. How can I calculate length of the page? e.g. I have 6 pages, length should be 6. I have tried but I am not getting length. I’m getting 0 as length.


<div class="col-md-12">
    <ul class="pagination">
        <li id="MainCopy_ctl19_Pager_liPrev" class="disabled">
            <a id="MainCopy_ctl19_Pager_PreviousPageButton" title="«" class="aspNetDisabled">«</a>
        <li> </li>
        <li id="MainCopy_ctl19_Pager_Repeater1_pager_li_0" class="active">
            <a id="MainCopy_ctl19_Pager_Repeater1_PageLink_0" class="aspNetDisabled">1 </a>
        <li id="MainCopy_ctl19_Pager_Repeater1_pager_li_1">
            <a id="MainCopy_ctl19_Pager_Repeater1_PageLink_1" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainCopy$ctl19$Pager$Repeater1$ctl01$PageLink&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))">2 </a>
        <li id="MainCopy_ctl19_Pager_Repeater1_pager_li_2">
            <a id="MainCopy_ctl19_Pager_Repeater1_PageLink_2" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainCopy$ctl19$Pager$Repeater1$ctl02$PageLink&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))">3 </a>
        <li id="MainCopy_ctl19_Pager_Repeater1_pager_li_3">
            <a id="MainCopy_ctl19_Pager_Repeater1_PageLink_3" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainCopy$ctl19$Pager$Repeater1$ctl03$PageLink&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))">4 </a>
        <li id="MainCopy_ctl19_Pager_Repeater1_pager_li_4">
            <a id="MainCopy_ctl19_Pager_Repeater1_PageLink_4" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainCopy$ctl19$Pager$Repeater1$ctl04$PageLink&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))">5 </a>
            <a id="MainCopy_ctl19_Pager_NextSectionButton" title="..." href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainCopy$ctl19$Pager$NextSectionButton&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))">...</a>
        <li id="MainCopy_ctl19_Pager_liNext">
            <a id="MainCopy_ctl19_Pager_NextPageButton" title="»" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainCopy$ctl19$Pager$NextPageButton&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))">»</a>

I have tried

paginat = len(browser.find_elements_by_xpath('//ul[@class="pagination"]/div/ul'))
for page1 in range(0,paginat):


The short answer is that you can’t… not from the first page. All you can tell at this point is the highest page number displayed but that will always be 5 if there are at least 5 pages. If that’s what you are looking for, you can use the code below to get that.

pages = browser.find_elements_by_css_selector("[id^='MainCopy_ctl19_Pager_Repeater1_PageLink_']")
count = pages[len(pages) - 1].text.strip()

If you really want to get the last page number, you’d have to click the » link until it doesn’t exist any more and then run the code above. If it’s got 100 pages, it’s going to take a while. You might look through the HTML or other parts of the page and see if there’s an indication of total number of pages.

The reason your code is returning 0 length is because your locator is not correct. There is no DIV or other UL under the pagination UL.


If you want to click the last page, use this code

pages = browser.find_elements_by_css_selector("[id^='MainCopy_ctl19_Pager_Repeater1_PageLink_']")
pages[len(pages) - 1].click()

You could insert the length into the ID but you don’t need to because you already have all the links, just click the last element in the collection and that will get you what you want.