Search code examples
pythonseleniumfindall

soup.find_all and find_elements_by_class return no elements found


I have tried two methods to find betting odd values with no results. I get nothing.

Here's the code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import pandas as pd

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.optibet.lv/sport/wcg/CS:GO-5541")

odds = driver.find_elements_by_class('event-block-row__odd event-block-row__odd_clickable event-block-row__odd_without-middle-odd')

if odds is not None:
    print('found odds element')
    print(odds)

This didn't work. It just prints 'found odds element'. I then tried to change the class name to odds = driver.find_elements_by_class('odd__value') to no avail. After that I tried to use BeautifulSoup:

from selenium import webdriver
from bs4 import BeautifulSoup

url = "https://www.optibet.lv/sport/wcg/CS:GO-5541"
driver = webdriver.Chrome()
driver.get(url)
soup = BeautifulSoup(driver.page_source, 'html.parser')

containers = soup.find_all("div", class_="event-block-row__odd event-block-row__odd_clickable event-block-row__odd_without-middle-odd")
print (len(containers))

This returns '0'. I'm out of ideas and not very experienced. Any help?


Solution

  • Switch to the iframe prior to getting the class. Then loop the list.

    driver.get("https://www.optibet.lv/sport/wcg/CS:GO-5541")
    driver.implicitly_wait(10)
    driver.switch_to.frame(driver.find_element_by_css_selector("#iFrameResizer0"))
    odds = driver.find_elements_by_class_name('odd__value')
    
    if odds is not None:
        print('found odds element')
        for odd in odds:
            print(odd.text)