Skip to content Skip to sidebar Skip to footer

Parsing Nested Elements Using Selenium Not Working - Python

The picture attached show the structure of the HTML page that I am trying to scrape: First I retrieve the element league-item and then I am looking for the i item with class name

Solution 1:

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']")

Solution 2:

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.

Post a Comment for "Parsing Nested Elements Using Selenium Not Working - Python"