Home > front end >  Have issues regarding how find_elements(By.CLASS_NAME, '') works
Have issues regarding how find_elements(By.CLASS_NAME, '') works

Time:11-16

I have some trouble understanding how Selenium's find_elements() method works. Almost each time I am trying to use it, it seems to return a list with a single value, namely only the first occurence of that element. Right now I am trying to scrape some betting websites as a small personal project. The endgoal is to build a program that takes the name of a football team as input and finds which betting company offers the best odds. My program succesfully reaches the page where the team's upcoming matches are listed, but then I encounter a problem. Here's my code:

Say this is the webpage I am trying to scrape:

link to the site i'm trying to scrape

I am noticing that all the matches information is under an element identifiable by the class = 'event-row-container'. There are multiple elements containing this class on the webpage. However:

matches = driver.find_elements(By.CLASS_NAME, "event-row-container")

for match in matches:
    print(match)

prints only one occurence of the element. What am I doing wrong?

CodePudding user response:

Try this:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#Define web driver as a Chrome driver and navigate
driver = webdriver.Chrome()
driver.maximize_window()

url = 'https://superbet.ro/cautare?query=Steaua'
driver.get(url)

# Click on accept cookies
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.ID, "onetrust-accept-btn-handler"))).click()

# Save all the events in a list
events = WebDriverWait(driver, 30).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "event-row-container")))

# Then with that list I do whatever I want, in this example only showing the text
for event in events:
    print(event.text)

As you can see I am using presence_of_element_located or presence_of_all_elements_located which is the best library.

I am not sure, but I would say find_elements will be deprecated

  • Related