-3

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

2
  • CLASS_NAME expects only one name but you have 3 names. But later Selenium converts it to CSS_SELECTOR so you may use . to join names (By.CLASS_NAME "match-team__score.match__score--home.u-font-weight-300") and it will convert it to correct CSS_SELECTOR.
    – furas
    Commented Jul 24 at 16:09
  • What would be really helpful is the URL for the website you're trying to scrape
    – Ramrab
    Commented Jul 24 at 16:43

1 Answer 1

1

By.CLASS_NAME wants a single classname, but you're passing 2 with a space between.

Since you only seem to care about the match-team__score--home part I'd just check on that class:

driver.find_element(By.CLASS_NAME, 'match-team__score--home')

If you want to check for both classes, I'd use By.CSS_SELECTOR so you can combine both classes into a selector.

driver.find_element(By.CSS_SELECTOR, 'div.match-team__score.match-team__score--home.u-font-weight-300')
2
  • div.match-team__score--home is NOT a class name... it's a CSS selector.
    – JeffC
    Commented Jul 24 at 19:13
  • 1
    Ahh you're completely right @JeffC, was too fast. I've improved my answer. Thanks for your comment!
    – 0stone0
    Commented Jul 25 at 13:56

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.