How can I click on a div button with selenium webdriver?

Oscar Ubillús picture Oscar Ubillús · Dec 19, 2016 · Viewed 18.9k times · Source

I have this button:-

But I tried with find element by classname:-

driver.findElementByClassName("dsk-col-1-4 card new").click();

But it does not work. Any help?

Answer

alecxe picture alecxe · Dec 19, 2016

The "by class name" locator usually expects a single class name to be passed:

driver.findElementByClassName("card").click();

If you want to use multiple classes, go with a "by CSS selector"

driver.findElementByCssSelector(".card.new").click();

Note that the dsk-col-1-4 class is not a very good choice for an element locator - this looks very much like a layout-oriented class name which not only have a higher probability to b changed, but also does not bring any information about the element and it's purpose. card and new on the other hand are a better fit.