如何在 Selenium 中提取 Web 元素的文本?
Selenium 使用 getText() 方法提取 Web 元素的文本。
使用 getText() 的代码实现。
示例
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;
public class ExtractText {
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(12, TimeUnit.SECONDS);
//Using id with # for css expression
driver.findElement(By.cssSelector("#gsc-i-id1")).sendKeys("Selenium");
// extracting the text entered in console with getText()
System.out.println(“The entered text is:” driver.findElement(By.cssSelector("#gsc-i- id1")).getText());
driver.close();
}
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP