简体   繁体   English

通过xpath获取div元素内的类名

[英]get the class name inside div element by xpath

I want to get the class name inside the div element by xpath. 我想通过xpath获取div元素内的类名。

<div class="indicator true"></div>

And I want to check if the class nams equals to "indicator true". 而且我想检查类名是否等于“ indicator true”。 Any help? 有什么帮助吗?

And I want to check if the class nams equals to "indicator true". 而且我想检查类名是否等于“ indicator true”。 Any help? 有什么帮助吗?

Yes, there is help. 是的,有帮助。

driver.findElement(By.xpath("//div[@class = 'indicator true']")) .click();

That said, I am not sure what your intention is. 也就是说,我不确定您的意图是什么。 Because you also say: 因为您还说:

I want to get the class name inside the div element by xpath. 我想通过xpath获取div元素内的类名。

The path expression above returns a div element, not the class name. 上面的路径表达式返回一个div元素,而不是类名。

You can access the class value of the div element by using the WebElement.getAttribute() - Method. 您可以使用WebElement.getAttribute()-方法访问div元素的类值。

Your Problem is to get the div element without the class as part of the selector. 您的问题是获取不带类的div元素作为选择器的一部分。

I suggest to add an id attribut to the div and select the div by id selector. 我建议为div添加一个id属性,并通过id选择器选择div。 Then you can check class value. 然后,您可以检查类的值。

WebElement divElem = driver.findElement(By.id("someId")) assert divElem.getAttribute("class").equals("indicator true");

//*[contains(concat(" ", normalize-space(@class), " "), " foo ")] // * [contains(concat(“”,normalize-space(@class),“”),“ foo”)]

source : Selecting a css class with xpath 源: 使用xpath选择一个CSS类

you can use the getAttribute() and enter the identify name in the brackets this will return you the identify value. 您可以使用getAttribute()并在方括号中输入标识名称,这将返回您的标识值。 the following code should return you "indicator true": 以下代码应返回“指标真”:

assert divElem.getAttribute("class");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM