Selenium Python css selector by class doesn't work

I have 4 ways to locate some element and want to click on it:

DOM is:

<div class="ui dropdown selection" tabindex="0">

And I locate this element by four ways:

(By.XPATH, "//div[@class='ui dropdown selection']")
(By.CSS_SELECTOR, "[class='ui dropdown selection']")
(By.CSS_SELECTOR, ".ui dropdown selection")
(By.CLASS_NAME, "ui dropdown selection")

I i just clik on element

Way 1 and 2 work, test is ok - and len(element) is 1

Way 3 and 4 don't work: NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".ui dropdown selection"} - and len(element) is 0 (Waits don't help, and Way 1, Way 2 don't require waits at all)

Could you tell me why Way 3 and Way 4 failed ?


When using multiple class names for the same tag within a CSS Selector, they must be separated by a dot instead of a space. Here's the correct way to express your third one:

(By.CSS_SELECTOR, ".ui.dropdown.selection")

OR

(By.CSS_SELECTOR, "div.ui.dropdown.selection").

As for the fourth one, you can't use By.CLASS_NAME with multiple class name components. You would have to pick one, but since that probably won't give you a unique selector, you'll be better off using one of the other ways to form a selector.