Home > Mobile >  Iterating Over WebElements
Iterating Over WebElements

Time:02-01

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  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  id="eventDetails_0">
      <div >
        <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"  style="width:95%"><b>AL Wakrah v Sadd</b></label>
          <label >
                                        31
                                        <label data-translate-set="DatePicker" data-translate-key="">
                                            Jan
                                        </label> 16:30 QAT Stars League
          </label>
        </a>
        <br>
        <label  style="padding-right:5px" data-translate-set="SportContent" data-translate-key="" data-event-status="1st half">1st half</label>
        <label  data-event-time=""> | 40:57 min </label>
        <label  data-event-score="0 - 0"> | 0 - 3</label>
      </div>
      <div >
        <div data-translate-set="SportContent" data-translate-key="" data-translate-type="title" title="Cashout is available for certains market on this event."  onclick="events.showTip()" data-toggle="tooltip"
          data-placement="auto left"></div>
      </div>
    </div>
    <div  data-elementtype="outcomeRow">



      <div >
        <div id="2148688e-e27f-ec11-80ef-00155d10891a" style=""  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" >AL Wakrah</div>


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

        

CodePudding user response:

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)

CodePudding user response:

@Prophet a time.sleep() still returns an empty list

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

(base) mac@noneofubusiness bookies_ke % python bookie1.py
[]

  •  Tags:  
  • Related