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

时光怂恿深爱的人放手 提交于 2020-02-02 00:30:07

问题


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?


回答1:


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.




回答2:


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!!




回答3:


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();



回答4:


Move to your element and click. Example:

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


来源:https://stackoverflow.com/questions/41230389/how-can-i-click-on-a-div-button-with-selenium-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!