Search code examples
pythonseleniumparsinghtml-parsing

Need help parsing html element and execute script no worked


<li tabindex="0" role="tab" aria-selected="false">
   <a href="#gift-cards" class="leftnav-links kas-leftnav-links" data-section="gift-cards" data-ajaxurl="/wallet/my_wallet.jsp">                         
   <span class="width200 kas-gift-cards-tab">Gift Cards</span>  
   <span class="count kas-count">info</span>
   </a>
</li>

I have such a html code which is duplicated on a page about 5 times, and only 2 of these blocks have information I need. Their classes are the same and I don't know what to do. Plus, the execute_script in Firefox does not work for me.

html_list = driver.find_element_by_id("rewards-contents")
                items = html_list.find_element_by_tag_name("li")
                for item in items:
                    text = item.text
                    print(text)

I tried to crank it on python, but nothing sensible came out.

I expect the script to display info from all 5 blocks.


Solution

  • To get all elements use find_elements instead of find_element. Your code should look like:

    html_list = driver.find_element_by_id("rewards-contents")
    items = html_list.find_elements_by_tag_name("li")
    for item in items:
        print(item.text)
    

    To get text by span elements:

    html_list = driver.find_elements_by_css_selector("#rewards-contents li")
    items = html_list.find_elements_by_tag_name("span")
    for item in items:
        print(item.text)