Oscar Ubillús
Oscar Ubillús

Reputation: 150

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

I have this button:-

 <div class="dsk-col-1-4 card new">
    <div class="div_center_div">
      <span class="icon icon_plus-black-symbol"></span>
      <h2>Create</h2>
    </div>
  </div>

But I tried with find element by classname:-

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

But it does not work. Any help?

Upvotes: 10

Views: 41575

Answers (4)

Gkumarap
Gkumarap

Reputation: 21

Move to your element and click. Example:

new Actions(driver).MoveToElement(yourElement).Click().Perform();

Upvotes: 2

Jsmith2800
Jsmith2800

Reputation: 1113

Within your locator you're passing multiple class names, and although they are both assigned to the element the findElementByClassName function realy only works when it is a single class name. The way I'd do it would be to use findelement(By.Xpath()), in this instance you'd need to use

webDriver.findElement(By.xpath("//div[contains(@class,'dsk-col-1-4 card new')]")).click();

Upvotes: 0

Arpan Buch
Arpan Buch

Reputation: 1400

Ok so I couldn't understand exactly Which element you want to click on, So based on my assumption , try below Xpaths :

1) if it is <div class="dsk-col-1-4 card new"> that you want to click

//div[contains(@class,'dsk-col-1-4 card new')]

2) If it is that you want to click,

//span[contains(@class,'icon icon_plus-black-symbol')]

3) If it is <h2>Create</h2> that you want to click,

//h2[text()='Create']

Hope this Helps!!

Upvotes: 1

alecxe
alecxe

Reputation: 473863

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.

Upvotes: 1

Related Questions