Selenium 中的 following-sibling 是什么?
我们可以使用 xpath 中的 following-sibling 概念来识别 Selenium 中的元素。它识别上下文节点的兄弟元素。兄弟元素应位于现有节点的同级,并且应具有相同的父元素。
让我们看一个具有 ul 标签的元素的示例,该元素有多个具有 li 标签的子元素。然后,让我们尝试从具有类属性 sreading 的第一个 li 元素中查找第五个 li 元素(撰写有效简历)。

语法
//li[@class='sreading']/following-sibling::li[4]
在此,我们正在查找 ul 标签的第五个子元素,但是我们提供了 li[4],因为我们正在从第一个子元素开始查找第五个子元素。

示例
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 FollowingSiblingXpath{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//implicit wait
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//URL launch
driver.get("https://tutorialspoint.com/about/about_careers.htm");
//identify fifth child from first child
WebElement n = driver.findElement
(By.xpath("//li[@class='sreading']/following-sibling::li[4]"));
String s = n.getText();
System.out.println("Text is: " + s);
driver.quit();
}
}输出

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