0

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.

0

2 Answers 2

0

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")))
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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.