0

I want to get the class name inside the div element by xpath.

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

And I want to check if the class nams equals to "indicator true". Any help?

user3409650
  • 505
  • 3
  • 12
  • 25
  • possible duplicate of [How to match attributes that contain a certain string?](http://stackoverflow.com/questions/1390568/how-to-match-attributes-that-contain-a-certain-string) – Alex Sep 26 '14 at 13:35

4 Answers4

0

And I want to check if the class nams equals to "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.

The path expression above returns a div element, not the class name.

Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
  • Actually I do not know how to get the class name and compare it by if statement. I edited my question. What I wrote was just a try I did. – user3409650 Sep 26 '14 at 13:46
0

You can access the class value of the div element by using the WebElement.getAttribute() - Method.

Your Problem is to get the div element without the class as part of the selector.

I suggest to add an id attribut to the div and select the div by id selector. Then you can check class value.

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

Robert
  • 230
  • 2
  • 9
  • It works!! thanks! Can I ask (just in case you might know) How can I get the value isnude the div element? for example how to get the value1 inside
    value1
    ?
    – user3409650 Sep 29 '14 at 16:52
  • use divElem.getText(), it returns the visible Text inside. – Robert Oct 06 '14 at 11:55
0

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

source : Selecting a css class with xpath

Community
  • 1
  • 1
Ashish
  • 735
  • 1
  • 6
  • 15
0

you can use the getAttribute() and enter the identify name in the brackets this will return you the identify value. the following code should return you "indicator true":

assert divElem.getAttribute("class");