遍历 WebElements

Iterating Over WebElements

我正在尝试遍历行 Web 元素,以获得赌注赔率,但我的代码一直给我一个空值

python代码:

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)

网站:https://www.betway.co.ke/Event/LiveSport

我曾尝试使用索引来获取值以及隐式等待,但仍然没有成功。 非常感谢您的帮助

标记如下:

  <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>
      

        

看起来您在 XPath 定位器中缺少一个点 . 来搜索每个 row 中的元素。否则它将 return 匹配 //div[contains(@class, "lipRowTopMargin")] 定位器的页面上的第一个元素。
看起来您还缺少 print.
的缩进 所以,而不是

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

请试试这个:

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