问题
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