Почему программа не видит данные блочного атрибута 'text'?

Пишу парсер для flashscore на Python. Не видит внутри блочного тега атрибут text (То есть не выводит данные). Пишу код jupyter notebook.

Вот мой код:

year= '2023'
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from datetime import datetime
from selenium.webdriver.common.action_chains import ActionChains
from collections import *
import numpy as np
import pandas as pd
import random as rd

s = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
driver.get(f'https://www.flashscore.com/volleyball/')
while True:
    try:
        time.sleep(3)
        driver.execute_script("windowsscrollTo(0, documentbody.scrollHeight);")
        time.sleep(3)
        driver.find_element_by_css_selector('a.event__more.event__more--static').click()
    except:
        break
match_id = driver.find_elements(by='css selector', value='.event__match.event__match--last.event__match--twoLine')
len (match_id)
match_id[0]
matches_id = [el.get_attribute('id') for el in match_id]
matches_id
matches_id = list(map(lambda x: x.replace('g_12_', ''), matches_id))
matches_id
game = matches_id[0]
url = 'https://www.flashscore.com/match' + game + '/#match-summary-summary/match-summary'
driver.get(url)
result = driver.find_elements(By.CSS_SELECTOR, '.smh__part  smh__home smh__part--1')
result = [0].text
AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_4532\3043893053.py in <module>
----> 1 result = [0].text 

Сообщение об ошибке:

AttributeError: 'list' object has no attribute 'text'

Вот скриншот ошибки:

Вот как выглядит блочный тег в devtool


Ответы (1 шт):

Автор решения: Aleksandr Fetisov

Проблема в том, что вы пытаетесь получить текст элемента, используя метод .text у списка result = [0], который не имеет атрибута text.

Должно быть что-то вроде

year= '2023'
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from datetime import datetime
from selenium.webdriver.common.action_chains import ActionChains
from collections import *
import numpy as np
import pandas as pd
import random as rd

s = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
driver.get(f'https://www.flashscore.com/volleyball/')
while True:
    try:
        time.sleep(3)
        driver.execute_script("windowsscrollTo(0, documentbody.scrollHeight);")
        time.sleep(3)
        driver.find_element_by_css_selector('a.event__more.event__more--static').click()
    except:
        break
match_id = driver.find_elements(by='css selector', value='.event__match.event__match--last.event__match--twoLine')
len(match_id)
match_id[0]
matches_id = [el.get_attribute('id') for el in match_id]
matches_id = list(map(lambda x: x.replace('g_12_', ''), matches_id))
game = matches_id[0]
url = 'https://www.flashscore.com/match' + game + '/#match-summary-summary/match-summary'
driver.get(url)
result = driver.find_elements(By.CSS_SELECTOR, '.smh__part  smh__home smh__part--1')
result = result[0].text

Конкретно изменена строка result = [0].text на result = result[0].text

→ Ссылка