Skip to content Skip to sidebar Skip to footer

Unable To Locate By Element By Class Name Using Selenium (Python)

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

Solution 1:

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

Solution 2:

In Selenium we do not have support for multiple class name, so basically we are left with only css and xpath in this kinda situation.

Since xpath is not viable solution in your case. try css.

You can combine multiple classes by removing the spaces in between, and simply putting . in that place.

Also, WebdriverWait will always return a common exception, and it's hard to figure out what exactly went wrong, so try using driver.find_element in case you want to have the proper error stack trace.

But for just that matter you should not remove WebdriverWait.

Try this :

score_block = self.driver.find_element_by_class_name("div.fl-l.score")

should work for you. if it works then replace with WebDriverWait


Post a Comment for "Unable To Locate By Element By Class Name Using Selenium (Python)"