Python Selenium find_element只按类查找每隔一个元素

afdcj2ne  于 2022-12-23  发布在  Python
关注(0)|答案(1)|浏览(106)

我想从这个页面得到每一种类型的赌注(三路,双机会等),但我的脚本只返回每第二个元素。

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.Chrome()

driver.get('https://sports.tipico.de/en/event/552146710?t=match')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#_evidon-accept-button'))).click()
time.sleep(2)

for odds_rows in driver.find_elements(By.XPATH, "//div[@class='OddGroups-styles-event-details-odds-groups']"):
    odds_row = odds_rows.find_element(By.CLASS_NAME, 'OddsRow-styles-odds-row')
    bet_type = odds_row.find_element(By.CLASS_NAME, 'OddsCaption-styles-caption-cell-title').text 
    print(bet_type)

结果:
三通
障碍(0:1)
高于/低于(1,5)
两队都得分?
谁赢了1.5分?
偶数/奇数?

4jb9z9bj

4jb9z9bj1#

现在从每个CollapsibleItem中选择了所有“OddGroups-styles-event-details-odds-groups”类div,您的XPATH不正确。请按如下方式更改脚本。

for odds_rows in driver.find_elements(By.XPATH, "//div[@class='OddGroups-styles-event-details-odds-groups']"):
    odds_row = odds_rows.find_element(By.CLASS_NAME, 'OddsRow-styles-odds-row')
    bet_type = odds_row.find_element(By.CLASS_NAME, 'OddsCaption-styles-caption-cell-title').text 
    print(bet_type)

for odds_rows in driver.find_element(By.XPATH, "//div[@class='ScoresOddsPage-styles-container']/div/div[2]/div").find_elements(By.CLASS_NAME, 'OddsRow-styles-odds-row'):
    bet_type = odds_rows.find_element(By.CLASS_NAME, 'OddsCaption-styles-caption-cell-title').text 
    print(bet_type)

相关问题