2

Here is the sample webpage code

<div class="size1of2 fllt">
    <div id="iad-service" class="tmargin2 rite fllt service-check"></div>
    <div class="fk-font-13 fk-font-regular">hi</div>
</div>

I want to find the "class" element using Selenium WebDriver.

Here is the code I tried.

String abc = driver.findElement(By.xpath("//div[contains(@id,'iad-service')]/@class")).getText();
System.out.println(abc);

When I tried this code(//div[contains(@id,'iad-service')]/@class) in the XPath Checker Addon, I am getting this output.

tmargin2 rite fllt service-check

But using WebDriver, I am getting an error. I want the output to be the content of the class which is.

tmargin2 rite fllt service-check

Where am I doing wrong?

6
  • what error log do you get from the execution? Commented Sep 30, 2014 at 17:17
  • has the html any frames? Commented Sep 30, 2014 at 17:37
  • @SatelliteSD NO frames.. Commented Sep 30, 2014 at 17:42
  • @guido Unable to find element //div[contains(@id,'iad-service')]/@class Commented Sep 30, 2014 at 17:43
  • do you wait until your element is present or are you trying to find it immediatly, taking the risk of that is isn't already loaded? Commented Sep 30, 2014 at 17:46

2 Answers 2

1

You need to fetch the div element, then retrive the class attribute value:

String abc = driver.findElement
               (By.xpath("//div[contains(@id,'iad-service')]")).getAttribute("class");
Sign up to request clarification or add additional context in comments.

Comments

0

You can use this:

WebElement id=wd.findElement(By.id("iad-service"));
    String className=id.getAttribute("class");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.