Python 获取与类名匹配的所有元素

Python 获取与类名匹配的所有元素,python,python-3.x,selenium,Python,Python 3.x,Selenium,我正在尝试获取上与给定类名匹配的所有元素。我尝试了driver.match_by_class_name,但奇怪的是只有第一个出现 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

我正在尝试获取上与给定类名匹配的所有元素。我尝试了
driver.match_by_class_name
,但奇怪的是只有第一个出现

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)

find\u element\u by\u class\u name
返回单个元素。要获取所有元素,您需要使用
按\u class\u name查找\u elements\u
,这将返回列表

您的代码应该如下所示:

driver.get(location)

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