1

I have this code:

HTML CODE

Then using selenium I write this:

driver.findElement(By.className("extra-delivery-item")).click();

The question is why does selenium tell me: no such element: Unable to locate element: {"method":"css selector","selector":".extra-delivery-item"

P.S. I have already located some buttons and successfully clicked on them, everything works, except that button.

4
  • Please don't post code as image: meta.stackoverflow.com/questions/285551/… Commented Jul 27, 2021 at 13:36
  • A shoot into the dark: On your image there are more than one element with this css class name. Commented Jul 27, 2021 at 13:40
  • Is this static HTML or are you dynamically modifying the DOM (like when using some sort of frontend framework)? Maybe you'll need some waits. Commented Jul 27, 2021 at 13:46
  • Just opened developer tool to search for buttons, then to click on them using Intellij idea + selenium Commented Jul 27, 2021 at 13:52

1 Answer 1

1

This element is actually has multiple class names. So to select it according to single class name you can use XPath or CSS selector.
Like this:

driver.findElement(By.xpath("//div[contains(@class,'extra-delivery-item')]")).click();

or

driver.findElement(By.cssSelector("div.extra-delivery-item")).click();

Selecting element by class name, like you tried to use

findElement(By.className("extra-delivery-item"))

means matching element with class attribute exactly matching that value, extra-delivery-item in this case. While the element class attribute here is extra-delivery-item extra-delivery-item-selected

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

21 Comments

Hello, ty for the answer, but selenium still throws exception
what exception?
Ok, hope you'll have a bit of time to look at it after your workday. I appreciate your time and advice, ty (;
Waiting for an element to load - this is essential here as I understood. So when I tried your option the element just did not loaded, that is why click failed.
Great! Happy I could be helpful.
|

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.