问题描述
我有这个按钮:-
<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>
但是我尝试用classname查找元素:-
But I tried with find element by classname:-
driver.findElementByClassName("dsk-col-1-4 card new").click();
但是它不起作用.有帮助吗?
But it does not work.Any help?
推荐答案
按类名"定位符通常希望传递单个类名:
The "by class name" locator usually expects a single class name to be passed:
driver.findElementByClassName("card").click();
如果要使用多个类,请使用通过CSS选择器"
If you want to use multiple classes, go with a "by CSS selector"
driver.findElementByCssSelector(".card.new").click();
请注意,对于元素定位器来说,dsk-col-1-4
类不是一个很好的选择-这看起来非常类似于面向布局的类名称,它不仅具有较高的概率被b更改,而且不会带来任何信息关于元素及其目的.另一方面,card
和new
更合适.
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.
这篇关于如何使用Selenium Webdriver单击div按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!