Search code examples
pythonhtml-listssplinter

Lists with splinter in Python 2.7


I am using splinter with Python 2.7, and from this code, I would like to get first_value = 1160, second_value = 829, and third_value = 0.

<ul id='resources'>
        <li id="metal_box" class="metal tooltipHTML" title="">
            <div class="resourceIcon metal"></div>
            <span class="value">
                <span id="resources_metal" class="">1.162</span>
            </span>
        </li>
        <li id="crystal_box" class="crystal tooltipHTML" title="">
            <div class="resourceIcon crystal"></div>
            <span class="value">
                <span id="resources_crystal" class="">831</span>
            </span>
        </li>
        <li id="energy_box" class="energy tooltipHTML" title="">
            <div class="resourceIcon energy"></div>
            <span class="value">
                <span id="resources_energy" class="">0</span>
                </span>
        </li>
  </ul>

I have tried different things, such as second_value = browser.find_by_id('resources').find_by_id('crystal_box').first.value, but I can't find out how make it work.

Thanks


Solution

  • Code

    browser.find_by_id('resources').find_by_id('crystal_box')
    

    will return li element that hasn't any value. If you want to dig to value, you must find last span element.

    element = browser.find_by_id('resources').find_by_id('crystal_box')
    value = element.find_by_tag('span').last.value