如何獲取 webdriver 中元素的當前內容?
我們可以在 Selenium webdriver 中獲取元素的當前內容。對於標籤名是 <input> 的元素,我們必須使用 getAttribute() 方法,並將值引數作為引數傳遞給該方法以獲取當前內容。
對於沒有輸入標籤的元素,我們必須使用 getText() 方法來獲取當前內容。首先,我們必須藉助定位器來識別元素。
讓我們嘗試獲取編輯框內 Selenium 的內容及其上方的文字內容——您正在瀏覽線上教育的最佳資源。

例子
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 ElementContent{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://tutorialspoint.tw/index.htm");
// identify elements
WebElement t = driver.findElement(By.cssSelector("h4"));
WebElement l = driver.findElement(By.id("gsc-i-id1"));
// getAttribute() with value for content in edit box
System.out.println("Content in edit box: " + l.getAttribute("value"));
//getText() to get content
System.out.println("Content in element: " + t.getText());
driver.close();
}
}輸出

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP