AxelotlZ
AxelotlZ

Reputation: 97

can't find element on webpage

I want to find all the elements with class name 'af24c._8125e' on this site: 'https://www.unibet.eu/betting/sports/filter/football/england/premier_league/matches' but when I run my following code:

 from selenium import webdriver
import pandas as pd

PATH = 'C:\Program Files (x86)\Chromedriver.exe'
driver = webdriver.Chrome(PATH)
URL = 'https://www.unibet.eu/betting/sports/filter/football/england/premier_league/matches'
driver.implicitly_wait(5)
driver.get(URL)
driver

# Teams vinden en list van maken
Teams = driver.find_element_by_class_name('af24c._8125e')
print(Teams)

It returns an empty list. I have tried waiting longer for the elements to load but that didn't work and also thought that maybe it didn't work because the elements were in a iframe or something, but I couldn't find it.

How can I find these elements?

Upvotes: 1

Views: 69

Answers (1)

Prophet
Prophet

Reputation: 33361

You are using a wrong locator.
Also with implicitly wait you will not surely get all the elements.
I would advise adding a small sleep after finding first team element to let all the other elements loaded.
Also you should use find_elements_by_class_name instead of find_element_by_class_name.
Also you need o extract the element text, not to print the element itself.
Try this:

from selenium import webdriver
import pandas as pd

PATH = 'C:\Program Files (x86)\Chromedriver.exe'
driver = webdriver.Chrome(PATH)
URL = 'https://www.unibet.eu/betting/sports/filter/football/england/premier_league/matches'
driver.implicitly_wait(5)
driver.get(URL)
driver

# Teams vinden en list van maken
#This will wait until first team element is found
driver.find_element_by_class_name('af24c')
#The sleep will ensure all the other elements are loaded
time.sleep(1)
#get all the teams elements
teams = driver.find_elements_by_class_name('af24c')
#Iterate over teams, extract each team element text
for team in teams:
   print(team.text)

Upvotes: 1

Related Questions