Selenium Can't find elements by CLASS_NAME

Question:

I’m trying to scrape a website and get every meal_box meal_container row in a list by driver.find_elements but for some reason I couldn’t do it. I tried By.CLASS_NAME, because it seemed the logical one but the length of my list was 0. Then I tried By.XPATH, and the length was then 1 (I understand why). I think I can use XPATH to get them one by one, but I don’t want to do it if I can handle it in a for loop.

I don’t know why the "find_elements(By.CLASS_NAME,’print_name’)" works but not "find_elements(By.CLASS_NAME,"meal_box meal_container row")"

I’m new at both web scraping and stackoverflow, so if any other details are needed I can add them.

Here is my code:

meals = driver.find_elements(By.CLASS_NAME,"meal_box meal_container row")
print(len(meals))

for index, meal in enumerate(meals):
    foods = meal.find_elements(By.CLASS_NAME, 'print_name')
    print(len(foods))
    
    if index == 0:
        mealName = "Breakfast"
    elif index == 1:
        mealName = "Lunch"
    elif index == 2:
        mealName = "Dinner"
    else:
        mealName = "Snack"
    for index, title in enumerate(foods):
        recipe = {}
        print(title.text)
        print(mealName + "n")
        recipe["name"] = title.text
        recipe["meal"] = mealName

Here is the screenshot of the HTML:
HTML

Asked By: Burak Saraç

||

Answers:

It seems Ok but about class name put a dot between characters.
Like "meal_box.meal_container.row" Try this.
meals = driver.find_elements(By.CLASS_NAME,"meal_box.meal_container.row")

Answered By: Fardad Ansari

Try to use driver.find_element_by_css_selector

Answered By: Lyn

It can be because "meal_box meal_container row" is inside of other element. So you should try finding the highest element and look for needed one inside.

root = driver.find_element(By.CLASS_NAME,"row")
meals = root.find_elements(By.CLASS_NAME, "meal_box meal_container row")
Answered By: Dzidzoiev Artem
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.