Unable to locate by element by class name using Selenium (Python)

Issue

I’m trying to locate a specific HTML element via class name but it’s not finding anything. I’ve looked into it for a while but I’m unable to identify the issue

HTML snippet: div’s class name is fl-l score

My Python Code:

# Throws Timeout Exception because element unable to be located.
WebDriverWait(self.driver, 5).until(ec.presence_of_element_located((By.CLASS_NAME, "fl-l score")))
score_block = self.driver.find_element_by_class_name("fl-l score")

I should also note that I tried finding it using XPath, which worked, but it’s not a viable solution because the website is dynamic.

Solution

Instead of finding element by class name, you can change the locator by css selector for multiple class names:

WebDriverWait(self.driver, 5).until(ec.presence_of_element_located((By.CSS_SELECTOR, ".fl-l.score")))
score_block = self.driver.find_element_by_css_selector(".fl-l.score")

The above code hits the page twice, to be more efficient you should compress it into one line, like so:

score_block = WebDriverWait(self.driver, 5).until(ec.presence_of_element_located((By.CSS_SELECTOR, ".fl-l.score")))

Answered By – frianH

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published