如何在 span 中获取文本 – Selenium
我们可以使用 Selenium 驱动程序获取 span 标签中找到的文本。
可以通过 getText 方法捕获一个 Web 元素的文本。让我们看一个包含文本的元素的示例 - © Copyright 2021. All Rights Reserved 封闭在 span 标签中。

语法
WebElement l = driver.findElement(By.xpath("//p/span"));
String s = l.getText();示例
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
public class SpanText{
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver",
"C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//implicit wait
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//maximize window
driver.manage().window().maximize();
//URL launch
driver.get("https://tutorialspoint.com/index.htm");
// scroll to element with Javascript Executor
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
// identify element with span and get text
WebElement l = driver.findElement(By.xpath("//p/span"));
String s = l.getText();
System.out.println("Text is: " + s);
driver.quit();
}
}输出

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