'Finding element with class name with spaces not working (selenium)

I'm trying to find this element inside a log-in page when a user inputs the wrong password

<div class="form-input-validation is-error" role="alert"></div>

I have tried

driver.find_element_by_css_selector(".form-input-validation.is-error")

but i get a 'no such element: Unable to locate element' error



Solution 1:[1]

You can find the element using the syntax, it is basically first waiting(using explicit wait) for the element to be located on the page and after that you can operate on it:

WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//div[@class='form-input-validation is-error']")))

Note: You have to add the following imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Solution 2:[2]

You just can substitute the space by a dot . to get the class.

driver.find_element_by_class_name("form-input-validation.is-error")

Solution 3:[3]

Class names can't have spaces, the div tag has two classes (separated by a space, .form-input-validation and is-error). Use one or the other, but I would suggest using .is-error because it will only activate if there is an error.

driver.find_element_by_css_selector(".is-error")

Solution 4:[4]

Try replacing the Space with a dot.

driver.find_element_by_class_name("form-input-validation.is-error")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Kevin M. Mansour
Solution 3 willactual
Solution 4 Phantomias