1

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 ?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Don't work please see answer below
1

@Michael

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

Both don't work - selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element

But notice that there is only 1 element because this works:

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

and len(element) = 1

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.