[Python] How to get text of a div element with selenium in python?

Discussão em 'Python' iniciado por Stack, Julho 24, 2025.

  1. Stack

    Stack Membro Participativo

    I keep trying to find the text of this div and it keeps coming up with this: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".match-team__score match__score--home u-font-weight-300"}

    • very new to web scraping so pls explain where im going wrong lol.

    my current code for this section is here:

    scorehome = match.find_element(By.CLASS_NAME, "match-team__score match__score--home u-font-weight-300")
    match_scorehome = scorehome.get_attribute("outerHTML")
    print(match_scorehome)
    scoreaway = match.find_element(By.CLASS_NAME, "match-team__score match__score--away u-font-weight-300")
    match_scoreaway = scoreaway.get_attribute("outerHTML")
    print(match_scoreaway)


    I've tried searching by xpath as well, same result 'no such element'

    for reference here is the website's code that i'm trying to get the text from:

    <div class="match-team__score match-team__score--home u-font-weight-300">
    <span class="u-visually-hidden">Scored</span>
    6
    <span class="u-visually-hidden">points</span>
    </div>


    the expected result should be '6' not any of the text from the spans.

    this also doesn't work, it just times out:

    match_scorehome = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class, 'match-team__score match__score--away u-font-weight-300')]"))).text print(match_scorehome)


    if this helps at all, here is the other related code:

    wait = WebDriverWait(driver, 30)
    match_elements = wait.until(EC.presence_of_all_elements_located((By.ID, "draw-content")))


    it goes into a for loop (for match in match_elements) and works for every other element (team names and date).

    Continue reading...

Compartilhe esta Página