your first trial,
driver.findElement(By.id("span1")).getAttribute("class");
looks good, did you try printing that ? if yes, did you get proper output or was there any other error ?
your 2nd code trial
driver.findElement.getAttribute(By.id("span1"));
is invalid, since you can not call getAttribute() like that, it should result in compile time error.
Your 3rd code trial :
driver.findElement("value").getAttribute(By.id("span1"));
is again invalid, you can not find the element with a string input.
your 4th code trial :
driver.findElement(By.id("span1")).getAttribute();
is again invalid, since you are not passing anything in getAttribute();
method. It should again result in compile time error.
Below is an alternative way with xpath :
//span[@id='span1']
and get the id or class attribute like this :
String idAttribute = driver.findElement(By.xpath("//span[@id='span1']")).getAttribute("id");
System.out.println(idAttribute)
or to get class attribute you can do something like this :
String classAttribute = driver.findElement(By.xpath("//span[@id='span1']")).getAttribute("class");
System.out.println(idAttribute)