1

Here is a screenshot of the html structure: enter image description here

I want to go through each item of league-list(every league-item) and look for the value aria-expanded.

Here is my code:

_1bet = 'https://1bet.com/ca/sports/tennis?time_range=all'
driver.get(_1bet) # enter the website
league1 = driver.find_elements_by_class_name('league-list')[0]  # first league-item
league1.find_element_by_xpath(".//div[@class='league-title.d-flex.justify-content-between.align-items-center.collapsible']")

Selenium can't find any element and I don't understand why. for reference I was inspired by another post : Parsing nested elements using selenium not working - python

1
  • 1
    @Prophet, sorry to bother again but If you can help It would be appreciated Commented Jul 24, 2021 at 19:54

1 Answer 1

1

First of all you have to add a wait to make a page loaded before accessing the elements there.
Then you can get all those elements into the list and then iterate over the list getting each element attribute.
I'm not sure the list is initially expanded as it is presented on your picture.
Also not sure all those league-title elements are matching the selector you provided.
If not - we can correct the code accordingly

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 20)

_1bet = 'https://1bet.com/ca/sports/tennis?time_range=all'
driver.get(_1bet) # enter the website
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.league-list")))
league1 = driver.find_elements_by_class_name('league-list')[0]  # first league-item
titles = league1.find_elements_by_xpath(".//div[@class='league-title.d-flex.justify-content-between.align-items-center.collapsible']")
for title in titles:
    print(title.get_attribute("aria-expanded"))
Sign up to request clarification or add additional context in comments.

2 Comments

it didn't work. It doesn't seem to find it as it returns an empty list for titles. I need to leave for now. will continue to test later. So I apologize if I don't answer soon
No problem. Just put a delay after driver.get(_1bet) something like time.sleep(10) and let me know if this worked. It's a problem that we can't access that page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.