Search code examples
python-2.7seleniumxpathrobotframeworkselenium2library

Get Matching Xpath Count Keyword does not return all matching Xpaths


I am trying to store a list of all radiobuttons within the page I am working on. The Radio Button amount will change depending on what pagination number I am on.

Right now I am using the code below to find all matching Xpaths and store it within a variable.

*** Keywords ***
Count How Many RadioButtons On Page
    ${Count}    Get Matching Xpath Count    //tr
    ${CountList}    Create List
    : FOR    ${INDEX}    IN RANGE    1    ${Count}
    \    Append To List    ${CountList}    xpath=//tr[${INDEX}]
    log    ${CountList}
    Set Global Variable    ${CountList}

This only stores 11 entries. When the page actually contains 20. For some reason it is missing the other 9 and I cannot figure out why.

At the end, the CountList variable looks like this:

u'xpath=//tr[1]', u'xpath=//tr[2]', u'xpath=//tr[3]', u'xpath=//tr[4]', u'xpath=//tr[5]', u'xpath=//tr[6]', u'xpath=//tr[7]', u'xpath=//tr[8]', u'xpath=//tr[9]', u'xpath=//tr[10]', u'xpath=//tr[11]'

This list should go all the way up to //tr[20]

I can also confirm that using (for example) //tr[12] or onwards, will work correctly. It is just that Get Matching Xpath Count is not pulling all 20 of them for some reason.

Can anyone shine light onto this?

EDIT:

Below is part of the HTML of the page I am trying to count:

<tbody id="oKGQf0-rows">
    <tr id="oKGQ_1" class="z-listitem z-listitem-selected">
        <td id="oKGQ0q" class="z-listcell">
            <div id="oKGQ0q-cave" class="z-listcell-content"><span id="oKGQ_1-cm" class="z-listitem-checkable z-listitem-radio"><i class="z-listitem-icon z-icon-radio"></i></span>&nbsp;P-DAC-15-790</div>
        </td>
        <td id="oKGQ1q" class="z-listcell">
            <div id="oKGQ1q-cave" class="z-listcell-content">DATA</div>
        </td>
        <td id="oKGQ2q" class="z-listcell">
            <div id="oKGQ2q-cave" class="z-listcell-content">..DATA</div>
        </td>
        <td id="oKGQ3q" class="z-listcell" title="AMD Contractors Work Permit">
            <div id="oKGQ3q-cave" class="z-listcell-content">15</div>
        </td>
        <td id="oKGQ4q" class="z-listcell">
            <div id="oKGQ4q-cave" class="z-listcell-content">i9</div>
        </td>
        <td id="oKGQ5q" class="z-listcell">
            <div id="oKGQ5q-cave" class="z-listcell-content">DATA</div>
        </td>
        <td id="oKGQ6q" class="z-listcell">
            <div id="oKGQ6q-cave" class="z-listcell-content">&nbsp;</div>
        </td>
        <td id="oKGQ7q" class="z-listcell">
            <div id="oKGQ7q-cave" class="z-listcell-content">26-06-2015 14:50</div>
        </td>
        <td id="oKGQ8q" class="z-listcell">
            <div id="oKGQ8q-cave" class="z-listcell-content">&nbsp;</div>
        </td>
        <td id="oKGQ9q" class="z-listcell">
            <div id="oKGQ9q-cave" class="z-listcell-content">&nbsp;</div>
        </td>
    </tr>
    <tr id="oKGQ11" class="z-listitem z-listbox-odd">
    </tr>
    <tr id="oKGQ31" class="z-listitem">
    </tr>
    <tr id="oKGQ51" class="z-listitem z-listbox-odd">
    </tr>
    <tr id="oKGQ91" class="z-listitem z-listbox-odd">
    </tr>
    <tr id="oKGQb1" class="z-listitem">
    </tr>
    <tr id="oKGQd1" class="z-listitem z-listbox-odd">
    </tr>
    <tr id="oKGQf1" class="z-listitem">
    </tr>
    <tr id="oKGQh1" class="z-listitem z-listbox-odd">
    </tr>
    <tr id="oKGQj1" class="z-listitem">
    </tr>
    <tr id="oKGQl1" class="z-listitem z-listbox-odd">
    </tr>
    <tr id="oKGQn1" class="z-listitem">
    </tr>
    <tr id="oKGQp1" class="z-listitem z-listbox-odd">
    </tr>
    <tr id="oKGQr1" class="z-listitem">
    </tr>
    <tr id="oKGQt1" class="z-listitem z-listbox-odd">
    </tr>
    <tr id="oKGQv1" class="z-listitem">
    </tr>
    <tr id="oKGQx1" class="z-listitem z-listbox-odd">
    </tr>
    <tr id="oKGQz1" class="z-listitem">
    </tr>
    <tr id="oKGQ02" class="z-listitem z-listbox-odd">
    </tr>
</tbody>

EDIT2:

Ive updated the Get Matching Xpath Count to //div[4]/table/tbody/tr to be a little more specific, and it still only counts 11, not 20

EDIT3:

If I perform the same test on the 2nd page, it works completely fine (returns 20, and loops through 20 rows) But there is no difference between page 1 and 2...


Solution

  • Turns out, RF didn't really understand what was going on within the page. So what I did was as I arrive to the page (via the nav bar) to then refresh the current page. It then saw all 20 entries and continued through the for loop as expected.

    I don't know if this is a bug within RF for Selenium2Library, But I shouldn't have needed to basically re-initialise the web page when trying to complete logic tasks.

    If someone else runs into a similar issue like this - comment below and ill try to assist you :)