我如何使用 Python 和 Selenium 遍历 webelements 列表?

新手上路,请多包涵

我想遍历一个 webelements 列表并返回每个元素的文本,但我只从第一个 <h2> 元素获取文本,而不是从另一个元素中的其余元素获取文本 <li> 标签,然后代码存在那个循环

这是我要从中提取文本的 Html 代码的一部分:

 <div class="KambiBC-event-page-component__column KambiBC-event-page-component__column--1">

            <ul class="KambiBC-list-view__column">
              <li class="KambiBC-bet-offer-category KambiBC-collapsible-container KambiBC-expanded KambiBC-bet-offer-category--hidden KambiBC-bet-offer-category--fade-in">
                <header class="KambiBC-bet-offer-category__header" data-touch-feedback="true">
                  <h2 class="KambiBC-bet-offer-category__title js-bet-offer-category-title">Piete selectate</h2>
                </header>
              </li>
              <li class="KambiBC-bet-offer-category KambiBC-collapsible-container KambiBC-expanded KambiBC-bet-offer-category--hidden KambiBC-bet-offer-category--fade-in">
                 <header class="KambiBC-bet-offer-category__header" data-touch-feedback="true">
                  <h2 class="KambiBC-bet-offer-category__title js-bet-offer-category-title">Another text</h2>
                 </header>
              </li>

              <li class="KambiBC-bet-offer-category KambiBC-collapsible-container KambiBC-bet-offer-category--hidden KambiBC-bet-offer-category--fade-in">
                 <header class="KambiBC-bet-offer-category__header" data-touch-feedback="true">
                  <h2 class="KambiBC-bet-offer-category__title js-bet-offer-category-title">Different text</h2>
                 </header>
             </li>

              <li class="KambiBC-bet-offer-category KambiBC-collapsible-container KambiBC-bet-offer-category--hidden KambiBC-bet-offer-category--fade-in">
                 <header class="KambiBC-bet-offer-category__header" data-touch-feedback="true">
                  <h2 class="KambiBC-bet-offer-category__title js-bet-offer-category-title">Yet another text</h2>
                 </header>
              </li>

            </ul>


      </div>

这是 Python 代码:

 import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Edge("D:\pariuri\python\MicrosoftWebDriver.exe")
driver.implicitly_wait(5)

driver.get("https://www.unibet.ro/betting#filter/football")

try:
    element_present = EC.presence_of_element_located((By.CLASS_NAME, 'KambiBC-event-result__score-list'))
    WebDriverWait(driver, 4).until(element_present)
except TimeoutException:
    print ('Timed out waiting for page to load')

event = driver.find_elements_by_class_name('KambiBC-event-item KambiBC-event-item--type-match')

for items in event:
   link = items.find_element_by_class_name('KambiBC-event-item__link')
   scoruri =  items.find_element_by_class_name('KambiBC-event-item__score-container')

   scor1 =  scoruri.find_element_by_xpath(".//li[@class='KambiBC-event-result__match']/span[1]")
   scor2 =  scoruri.find_element_by_xpath(".//li[@class='KambiBC-event-result__match']/span[2]")

   print (scor1.text)
   print (scor2.text)
   if scor1.text == '0' and scor2.text == '0':


        link.click()
        time.sleep(3)

        PlajePariuri = driver.find_elements_by_xpath("//ul[@class='KambiBC-list-view__column']")
        for items in PlajePariuri:
             NumePlaje = items.find_element_by_xpath("//li/header/h2")
             print (NumePlaje.text)




原文由 Rius2 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 731
2 个回答

它一直在我的脸上,这将打印每个元素的文本,很高兴我能找到

PlajePariuri = driver.find_elements_by_class_name('KambiBC-bet-offer-category KambiBC-collapsible-container KambiBC-expanded KambiBC-bet-offer-category--hidden KambiBC-bet-offer-category--fade-in')

    for items2 in PlajePariuri:

        NumePlaje = items2.find_element_by_class_name('KambiBC-bet-offer-category__title js-bet-offer-category-title')

        print (NumePlaje.text)

原文由 Rius2 发布,翻译遵循 CC BY-SA 3.0 许可协议

试试下面的代码-

 PlajePariuri = driver.find_elements_by_xpath("//ul[@class='KambiBC-list-view__column']//li/header/h2")
for items in PlajePariuri:
    print (items.text)

原文由 Kapil 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏