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.tw/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();
   }
}

輸出

更新於:06-4 月-2021

超過 23K 瀏覽量

開啟你的 職業生涯

完成本課程獲得認證

立即開始
廣告
© . All rights reserved.