如何使用 Selenium 获取 css 类名称?
我们可以使用 Selenium webdriver 获取某个元素的 css 类名称。如需获取 html 文档中某个元素的类名称属性,必须使用 getAttribute() 方法。然后将 class 值作为参数传给方法。
让我们考虑一下带有类属性的以下 html 代码。

类属性的 value 值为 gsc-input。可以使用 getAttribute() 方法获取该值。
示例
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class GetClssAttribute{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://tutorialspoint.com/index.htm";
driver.get(url);
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
// identify element
WebElement l = driver.findElement(By.id("gsc-i-id1"));
// get class attribute with getAttribute()
String val = l.getAttribute("class");
System.out.println("Class attribute value: " + val);
driver.quit()
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP