如何在 Selenium 中提取元素的屬性值?
藉助 getAttribute() 方法,我們可以在 Selenium 中提取元素的屬性值。找到元素後,使用此方法來獲取元素的屬性值並分配給一個變數。
示例
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import java.util.List;
public class AttributeType {
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.tw/index.htm";
driver.get(url);
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
//Using id tagname attribute combination for css expression
WebElement button = driver.findElement(By.cssSelector("input[name=’search’]"));
// getting the type attribute and printing in console
String buttontype = button.getAttribute(“type”);
System.out.println(“Attribute value is “ + buttontype);
driver.close();
}
}
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP