Home > front end >  Python Selenium find_element only finds every second element by class
Python Selenium find_element only finds every second element by class

Time:12-19

I want get every type of bet from this page (3-Way, Double Chance, etc.) but my script only returns every second element.

enter image description here

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)

Result:

3-Way

Handicap (0:1)

Over/Under (1,5)

Both teams to score?

Who wins the 1.half?

Even/Odd?

CodePudding user response:

Your XPATH is incorrect now selected all 'OddGroups-styles-event-details-odds-groups' class divs from each CollapsibleItem. Please change your script like this.

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)

to

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)
  • Related