1

Just as the title implies I'm actually trying to get all the li elements from a dropdown field and actually be able to compare those results to the ones from an specific .txt file.

The html/DOM contains this kind of list

< div id = "ProgramCategoriesAndCodes_chzn" class = "chosen-container groups-are-selectable show-selected-in-list chosen-container-multi modified-chzn chosen-container-active" style = "width: 291px;" > <ul class="chosen-choices">
    <li class="search-field">
        <input type="text" value=" " class="default" autocomplete="off" style="width: 266px;"></li>
    </ul>
    <div class="chosen-drop" style="left: 0px; width: 291px; top: 29px;">
        <ul class="chosen-results">
            <li id="ProgramCategoriesAndCodes_chzn_g_0" class="group-result active-result child-matched" title="AA - AEROBICS">AA - AEROBICS</li>
            <li id="ProgramCategoriesAndCodes_chzn_o_1" class="active-result group-option" style="" title="AABD - BODYSHAPING WITH CORY EVERSON">AABD - BODYSHAPING WITH CORY EVERSON</li>
            <li id="ProgramCategoriesAndCodes_chzn_o_2" class="active-result group-option" style="" title="AABP - Cory Everson's Gotta Sweat">AABP - Cory Everson's Gotta Sweat</li>
            <li id="ProgramCategoriesAndCodes_chzn_o_3" class="active-result group-option" style="" title="AABS - Bodyshaping">AABS - Bodyshaping</li>
            <li id="ProgramCategoriesAndCodes_chzn_o_4" class="active-result group-option" style="" title="AACF - Crunch Fitness">AACF - Crunch Fitness</li>
            <li id="ProgramCategoriesAndCodes_chzn_o_5" class="active-result group-option" style="" title="AACJ - City Jam">AACJ - City Jam</li>
            <li id="ProgramCategoriesAndCodes_chzn_o_6" class="active-result group-option" style="" title="AADA - GETTING FIT WITH DENISE AUSTIN">AADA - GETTING FIT WITH DENISE AUSTIN</li>
            <li id="ProgramCategoriesAndCodes_chzn_o_7" class="active-result group-option" style="" title="AAFA - Fitness America Tour">AAFA - Fitness America Tour</li>
            <li id="ProgramCategoriesAndCodes_chzn_o_8" class="active-result group-option" style="" title="AAKI - KIANA'S FLEX APPEAL">AAKI - KIANA'S FLEX APPEAL</li>
            <li id="ProgramCategoriesAndCodes_chzn_g_9" class="group-result active-result child-matched" title="ABB - ESPN Radio Baseball">ABB - ESPN Radio Baseball</li>
            <li id="ProgramCategoriesAndCodes_chzn_o_10" class="active-result group-option" style="" title="ABBAS - MLB All Star Game">ABBAS - MLB All Star Game</li>
            <li id="ProgramCategoriesAndCodes_chzn_o_11" class="active-result group-option" style="" title="ABBDC - Series Del Caribe">ABBDC - Series Del Caribe</li>
            <li id="ProgramCategoriesAndCodes_chzn_o_12" class="active-result group-option" style="" title="ABBDS - MLB Division Series">ABBDS - MLB Division Series</li>
            <li id="ProgramCategoriesAndCodes_chzn_o_13" class="active-result group-option" style="" title="ABBLC - MLB League Championships">ABBLC - MLB League Championships</li>
            <li id="ProgramCategoriesAndCodes_chzn_o_14" class="active-result group-option" style="" title="ABBMA - MLB Meet the Stars">ABBMA - MLB Meet the Stars</li>
            <li id="ProgramCategoriesAndCodes_chzn_o_15" class="active-result group-option" style="" title="ABBML - MLB Regular">ABBML - MLB Regular</li>
            <li id="ProgramCategoriesAndCodes_chzn_o_16" class="active-result group-option" style="" title="ABBMS - MLB Specials">ABBMS - MLB Specials</li>
            <li id="ProgramCategoriesAndCodes_chzn_o_17" class="active-result group-option" style="" title="ABBSN - MLB Sunday Night Baseball">ABBSN - MLB Sunday Night Baseball</li>
            <li id="ProgramCategoriesAndCodes_chzn_o_18" class="active-result group-option" style="" title="ABBWB - World Baseball Classic">ABBWB - World Baseball Classic</li>
            <li id="ProgramCategoriesAndCodes_chzn_o_19" class="active-result group-option" style="" title="ABBWS - MLB World Series">ABBWS - MLB World Series</li>
            <li id="ProgramCategoriesAndCodes_chzn_g_20" class="group-result active-result child-matched" title="ABK - Deportes Radio Basketball">ABK - Deportes Radio Basketball</li>
            <li id="ProgramCategoriesAndCodes_chzn_o_21" class="active-result group-option" style="" title="ABKAS - NBA All Star Game">ABKAS - NBA All Star Game</li>
        </ul>
    </div>
</div>

The only thing I have so far (since I'm quite a noob at Selenium and Python), is this:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get(url)

elem = driver.find_element_by_xpath('//div[@id='ProgramCategoriesAndCodes_chzn']//ul[@class='chosen-results']

list_elem = elem.find_elements_by_tag_name("li")
for li in list_elem:
     text = li.text
     print(text)

I know that print doesn't actually serves any purpose at all but I'm lost here on how to actually achieve what I asked in first place :(

The actual text file contains info like this one:

AEROBICS
              AE - AEROBICS
              AE - BODYBUILDING
              AE - MISCELLANEOUS
AMERICAS CLUB SOCCER
              SOAM - ARGENTINE SOCCER
              SOAM - ARGENTINE SOCCER RPT
              SOAM - BRASILEIRAO SUB 20
              SOAM - BRASILEIRAO SUB 20 RPT
              SOAM - BRAZILIAN CHAMPIONSHIP
              SOAM - BRAZILIAN CHAMPIONSHIP RPT
              SOAM - COPA DO BRAZIL
              SOAM - COPA DO BRAZIL RPT
              SOAM - COPA MEXICO
              SOAM - COPA MEXICO RPT
              SOAM - COPA SAO PAULO JUNIOR
              SOAM - LIGA FPD DE COSTA RICA
              SOAM - LIGA MEXICANA
              SOAM - LIGA MEXICANA RPT
              SOAM - MAJOR LEAGUE SOCCER
              SOAM - MAJOR LEAGUE SOCCER RPT
              SOAM - USA SOCCER

Any help would be greatly appreciated!! Kind Regards.

vitaliis
  • 4,082
  • 5
  • 18
  • 40
Esepee
  • 87
  • 1
  • 7
  • Check here how to compare two lists in Python https://stackoverflow.com/questions/1388818/how-can-i-compare-two-lists-in-python-and-return-matches – vitaliis May 29 '21 at 03:32
  • @vitaliis thanks man, will try this along with the solution that gaurav indicated below! – Esepee May 29 '21 at 14:04

1 Answers1

0

I am assuming you are having problem to how to match with txt file because you are able to extract all text from li elements so i will suggest you to make list of your txt file like this

with open('test.txt','r') as f:
    txt_list = f.read().splitlines() # it will give list of each line
    print(list(x.strip() for x in txt_list)) # it will strip the white space from start and end

now you have two lists one form li and other from txt now you can use python in or set as your requirement.

other thing can be use is you simply match your txt from li to text file string name = 'AE - AEROBICS'

name = 'AE - AEROBICS'
with open('test.txt','r') as f:
    txt_str = f.read() # return txt file as str
    print(txt_str.find(name)) $ return index if present otherwise -1 
gaurav
  • 1,281
  • 1
  • 13
  • 25