Revolucion for Monica
Revolucion for Monica

Reputation: 3306

Get all elements that match a class name

I'm trying to get all elements that match a given classname on this webpage. I tried driver.match_by_class_name but weirdly enough only the first one show up.

from collections import defaultdict
import json

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import ElementClickInterceptedException

import time

driver = webdriver.Chrome(executable_path=r"C:\Programs\chromedriver.exe")
location = "https://docs.google.com/spreadsheets/d/1iLqEFRaHPYxpJKU05VXt3HUCQ2OQUAg8FfWlyFbvaXc/edit?usp=sharing"
location = "https://docs.google.com/forms/d/e/1FAIpQLSfzocEm6IEDKVzVGOlg8ijysWZyAvQur0NheJb_I_xozgKusA/viewform?usp=sf_link"

class_name = "freebirdFormviewerViewItemsItemItemTitle.exportItemTitle.freebirdCustomFont"

driver.get(location)

questions = driver.find_element_by_class_name(class_name)
print(questions.text)

Upvotes: 0

Views: 1678

Answers (1)

Sameer Arora
Sameer Arora

Reputation: 4507

find_element_by_class_name returns a single element. To get all the elements you need to use find_elements_by_class_name, which would be returning the list.

Your code should be like:

driver.get(location)

all_questions = driver.find_elements_by_class_name(class_name)
for question in all_questions:
    print(question.text)

Upvotes: 1

Related Questions