Search code examples
pythonseleniumempty-list

Iterating Over WebElements


I'm trying to iterate over row web elements, so as to get bookie odds but my code keeps giving me an empty

python code:

dropdown = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="filtermarket-button"]'))).click()

dropdown = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,
  '/html/body/div[2]/div[3]/div[5]/'
  'div/div[3]/div/div[2]/div[2]/div/div/'
  'div[1]/div[2]/div[2]/div/ul/li[1]')))

dropdown.click()
driver.implicitly_wait(10)
box = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, '//div[@id="subsection"]//div[@id="matchbetting-events"]')))
xpath_str = "//div[@class='row' and @class='eventRow']"
rows = box.find_element(By.XPATH, '//div[contains(@class, "active")]').find_elements(By.XPATH, xpath_str)
for row in rows:
  odds = row.find_element(By.XPATH, './/div[contains(@class, "lipRowTopMargin")]')
  odds_events.append(odds.text)
print(odds_events)

Website: https://www.betway.co.ke/Event/LiveSport

I have tried to use an index to get the values as well as implicit waits, but, still no luck. Your help would be much appreciated

markup is as follows:

  <div class="row eventRow" id="01e346b7-0000-0000-0000-000000000000" data-event-id="01e346b7-0000-0000-0000-000000000000" data-markettitle="Match Result (1X2)" data-eventtitle="AL Wakrah v Sadd" data-sporttitle="Soccer" data-eventdate="2022-01-31 16:30"
    data-value="01e346b7-0000-0000-0000-000000000000" data-market="12e0c327-0000-0000-0000-000000000000" data-markettype="de677d0c-e70f-e811-80d9-00155d4cf18a" data-markettypecategory="00000000-0000-0000-da7a-000000580003">
    <div class="col-xs-9 col-sm-10 col-md-10 col-lg-4 eventDetails padding--none" id="eventDetails_0">
      <div class="inplayStatusDetails col-xs-10 col-md-11 col-lg-11">
        <a rel="nofollow" href="/sport/soccer/qat/stars_league/al_wakrah-v-sadd/0?inplay=true&amp;datefilter=202201311630">
          <label data-translate-type="event" data-translate-set="Soccer" data-translate-market="Match Result (1X2)" data-translate-key="AL Wakrah v Sadd" class="ellips theOtherFont" style="width:95%"><b>AL Wakrah v Sadd</b></label>
          <label class="ellips theOtherFont liveEllips label__league_title">
                                        31
                                        <label data-translate-set="DatePicker" data-translate-key="">
                                            Jan
                                        </label> 16:30 QAT Stars League
          </label>
        </a>
        <br>
        <label class="pull-left label__live_status" style="padding-right:5px" data-translate-set="SportContent" data-translate-key="" data-event-status="1st half">1st half</label>
        <label class="pull-left label__live_status" data-event-time=""> | 40:57 min </label>
        <label class=" pull-left label__live_status" data-event-score="0 - 0"> | 0 - 3</label>
      </div>
      <div class="col-xs-2 col-md-1 col-lg-1 event-icons no-sidePaddings">
        <div data-translate-set="SportContent" data-translate-key="" data-translate-type="title" title="Cashout is available for certains market on this event." class="co-img co-img-white pull-right mm-co toolTipIcon" onclick="events.showTip()" data-toggle="tooltip"
          data-placement="auto left"></div>
      </div>
    </div>
    <div class="col-xs-12 col-md-12 col-lg-7 outcomes lipRowTopMargin " data-elementtype="outcomeRow">



      <div class="outcomeBtnLong outcomebuttonDiv">
        <div id="2148688e-e27f-ec11-80ef-00155d10891a" style="" class="btn btn-group btn-bettingmatch inplay" data-sortindex="0" data-lip-sbv="None" onclick="gax.SetBetPOI('Sport Page', this.id);
    SendToBetslip
    (
        this.id,
        'AL Wakrah',
        '9.2500',
        'Soccer',
        'Match Result (1X2)',
        'None',
        'AL Wakrah v Sadd',
        '1/31/2022 4:30:00 PM',
        '01e346b7-0000-0000-0000-000000000000',
        'True',
         true,
         false,
         'Stars League'
      ); ">

          <div data-translate-type="outcome" data-translate-set="Soccer" data-translate-market="Match Result (1X2)" data-translate-key="AL Wakrah" class="outcome-title text-left  ">AL Wakrah</div>


          <div class="outcome-pricedecimal" data-lip-value="2148688e-e27f-ec11-80ef-00155d10891a" data-lip-pricedecimal="50.00" style="">50.00</div>
        </div>
      </div>
      

        


Solution

  • Looks like you are missing a dot . in your XPath locator to search for an element inside each row. Without that it will return the first element on the page matching //div[contains(@class, "lipRowTopMargin")] locator.
    Also looks like you are missing indentation for the print.
    So, instead of

    for row in rows:
      odds = row.find_element(By.XPATH, '//div[contains(@class, "lipRowTopMargin")]')
    print(odds.text)
    

    Please try this:

    for row in rows:
      odds = row.find_element(By.XPATH, './/div[contains(@class, "lipRowTopMargin")]')
      print(odds.text)