lalaland
lalaland

Reputation: 439

Parsing nested elements using selenium not working - python

The picture attached show the structure of the HTML page that I am trying to scrape: enter image description here

First I retrieve the element league-item and then I am looking for the i item with class name : 'ds-icon-material league-toggle-icon'

Selenium is telling me that it cannot find any item with such name. Here is my code:

path = r"""chromedriver.exe"""
driver = webdriver.Chrome(executable_path=path)
driver.get(_1bet)
time.sleep(5)
#a = driver.find_element_by_class_name('box-content.box-bordered.box-stick.box-bordered-last')

league1 = driver.find_elements_by_class_name('league-list')[0]
league1.find_element_by_class_name("ds-icon-material league-toggle-icon")

Can you please help me? I dont understand why it isn't working.

Thanks

NB: The website I'm scraping is: https://1bet.com/ca/sports/tennis?time_range=all

Upvotes: 1

Views: 1914

Answers (2)

Prophet
Prophet

Reputation: 33381

I can't access that web page so I can only guess what is going there.
I can figure 2 problems here:

  1. To select element inside element it's better to use XPath starting with a dot .
  2. The element you trying to access having 2 class names. You should use css selector or XPath to locate element according to multiple class names.
    So I suggest you trying this:
league1 = driver.find_elements_by_class_name('league-list')[0]
league1.find_element_by_xpath(".//i[@class='ds-icon-material league-toggle-icon']")

Upvotes: 4

furas
furas

Reputation: 143187

Selenium expects single class name - and it adds dot at the beginning to create CSS selector.
But "ds-icon-material league-toggle-icon" is two classes and it will add dot befor first class but not before second class and this makes proble.

You may use directly css selector with all dots

 .find_element_by_css_selctor(".ds-icon-material.league-toggle-icon")

or you have to trick Selenium and add missing dots between classes

 .find_element_by_class_name("ds-icon-material.league-toggle-icon")

I can't connect with this page to confirm that this is all.

Upvotes: 1

Related Questions