What's the difference between functions getClass() and getAttribute(“class”) in Selenium?

0 votes

Kindly check this code

`<div class="datepicker dropdown-menu" style="display: block; top: 301px; left: 308px;">
<div class="datepicker-days" style="display: block;">
<div class="datepicker-months" style="display: none;">
<div class="datepicker-years" style="display: none;">
</div>`

I'm trying get the classname of Divs present inside Div with classname(datepicker dropdown-menu") but it work with  wdriver.getAttribute("class").toString(); and not with wdriver.getclass().toString();. Can anybody tell me what is the difference?

Apr 7, 2018 in Selenium by Martin
• 4,210 points
1,846 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

The simple difference is that, getClass() returns the XPath of the webelement. Whereas, getAttribute("class") will return the value of the class attribute within the <div> tags. Look at the code below to understand this concept better.

    WebElement ele = driver.findElement(By.xpath(".//*[@id='next']"));
    String a = ele.getAttribute("class");
    System.out.println(a);
    string b = ele.getClass();
    System.out.println(b);

And if the html code was this:-

<input id="next" class="rc-button rc-button-submit" type="submit" value="Next" name="signIn"/>

Then, a will return: "rc-button rc-button-submit". Whereas, b will return: ".//*[@id='next']"

Hope the explanation was clear.

answered Apr 7, 2018 by nsv999
• 5,030 points

Related Questions In Selenium

0 votes
1 answer

What is the difference between findElement and findElements in Selenium Webdriver?

Hi Nilaabh, Selenium Webdriver uses findElement and ...READ MORE

answered May 8 in Selenium by Anvi
• 5,960 points
37 views
0 votes
1 answer
0 votes
1 answer

Difference between Action and Actions in Selenium

Action is an interface : public interface Action Action Interface represents ...READ MORE

answered Apr 9, 2018 in Selenium by Martin
• 4,210 points
2,013 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,030 points
73 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
446 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
55 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,030 points
3,525 views
0 votes
1 answer

What's the diff between Selenium RC and Selenium WebDriver?

RC works by injecting the JavaScript functions ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,030 points
124 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.