XPathに含まれるもの: テキスト、次の兄弟と祖先 Selenium
🚀 スマートサマリー
XPathのContains、Sibling、およびAncestor Selenium 構造化された関係とテキストパターンを用いて、Web要素を正確に識別できます。これらのXPath関数は、複雑なDOM階層における自動化の信頼性、柔軟性、保守性を向上させます。
XPath には何が含まれますか?
XPath には以下が含まれます はXPath式内の関数で、特定のテキストを含むWeb要素を検索するために使用されます。XPathのcontains()関数を使用することで、Webページ全体から指定されたテキスト値に一致するすべての要素を抽出できます。XPathのcontains関数は、部分的なテキストを含む要素を検索できます。
例 - テキストが含まれています
ここでは、アンカーを検索しています。テキストには「SAP M'。
"//h4/a[contains(text(),'SAP M')]"
注: 次のXPath演習をこの上で練習することができます https://demo.guru99.com/test/selenium-xpath.html
単純な場合 XPath テストスクリプト用の複雑なWeb要素が見つからない場合は、XPath 1.0ライブラリの関数を使用する必要があります。これらの関数を組み合わせることで、より具体的なXPathを作成できます。
👉 無料ライブ登録 Selenium テストプロジェクト
XPath で兄弟をフォローする
A 兄弟姉妹 Selenium ウェブドライバー は、親要素の兄弟であるWeb要素を取得するために使用される関数です。親要素が既知の場合、Web要素は簡単に検索または位置指定することができ、XPath式の兄弟属性を使用することができます。 Selenium ウェブドライバー。
XPath 内の兄弟の例:
ここでは、「a」の兄弟要素に基づいて「h4」を見つけています。
"//div[@class='canvas- graph']//a[@href='/accounting.html'][i[@class='icon-usd']]/following-sibling::h4"
祖先: 親要素に基づいて要素を検索するには、XPath のancestor属性を使用できます。
例を使ってこれら3つの機能を理解しましょう。
テスト手順:
注意: チュートリアルの作成日以降、Guru99のホームページは更新されているので、代わりにデモサイトを使用してテストを実行してください。
- に行く https://demo.guru99.com/test/guru99home/
- 「最も人気のあるコース」セクションで、テキストが「SELENIUM」であるWeb要素の兄弟であるすべてのWeb要素を検索します。
- XPath テキストの内容、祖先、兄弟関数を使用して要素を検索します。
USING にはテキストと XPath の兄弟が含まれます
import java.time.Duration;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.Test;
// If you prefer WebDriverManager (optional):
// import io.github.bonigarcia.wdm.WebDriverManager;
public class SiblingAndParentInXpath_Chrome {
@Test
public void testSiblingAndParentInXpath() {
// === Option A: Use local ChromeDriver binary path ===
// Update this path to your chromedriver location:
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
// === Option B: Use WebDriverManager (uncomment next line and remove Option A lines) ===
// WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
// Add any flags you need, e.g. headless:
// options.addArguments("--headless=new");
WebDriver driver = new ChromeDriver(options);
try {
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().window().maximize();
driver.get("https://demo.guru99.com/test/guru99home/");
// Find all siblings (divs) next to the 'SELENIUM' tile within
// the "A few of our most popular courses" section.
// Steps encoded in XPath:
// 1) Locate the H2 that contains the section title
// 2) Move to its parent DIV
// 3) Inside it, locate the link with text 'SELENIUM'
// 4) From the SELENIUM tile's parent DIV, get following sibling tiles
List<WebElement> dateBox = driver.findElements(By.xpath(
"//h2[contains(., 'A few of our most popular courses')]/parent::div" +
"//a[normalize-space(.)='SELENIUM']/parent::div" +
"/following-sibling::div[contains(@class,'rt-grid-2')]"
));
// Print the text of each sibling element
for (WebElement el : dateBox) {
System.out.println(el.getText());
}
} finally {
driver.quit();
}
}
}
出力は次のようになります:
XPath の祖先 Selenium
XPath の祖先 Selenium は、指定されたレイヤーにある特定の要素の祖先を検索する関数です。返される祖先の階層、またはメンバーの階層に対する祖先の階層を明示的に指定できます。祖先からの階層ステップ数を返し、ユーザーが求める祖先を特定します。
ここで、テキストが「SELENIUM」であるアンカーの祖先の助けを借りて、「人気コース」セクションのすべての要素を検索する必要があるとします。
ここでの xpath クエリは次のようになります
"//div[.//a[text()='SELENIUM']]/ancestor::div[@class='rt-grid-2 rt-omega']/following-sibling::div"
完全なコード
import java.time.Duration;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.Test;
public class AncestorInXpath_Chrome {
@Test
public void testAncestorInXpath() {
// Set path to your ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
WebDriver driver = new ChromeDriver(options);
try {
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().window().maximize();
driver.get("https://demo.guru99.com/test/guru99home/");
// Search all elements in 'Popular course' section
// using the ancestor of the 'SELENIUM' link
List <WebElement> dateBox = driver.findElements(
By.xpath("//div[.//a[text()='SELENIUM']]/ancestor::div[@class='rt-grid-2 rt-omega']/following-sibling::div")
);
// Print all sibling elements of the 'SELENIUM' tile
for (WebElement element : dateBox) {
System.out.println(element.getText());
}
} finally {
driver.quit();
}
}
}
出力は次のようになります-
AND と OR の使用
AND と OR を使用すると、XPath 式に 2 つの条件を設定できます。
- AND の場合、2 つの条件が両方とも真である場合にのみ要素が見つかります。
- OR の場合、2 つの条件のうちいずれか 1 つが真である場合にのみ要素が検索されます。
ここで、XPathクエリは次のようになります。
Xpath=//*[@type='submit' OR @name='btnReset']
Xpath=//input[@type='submit' and @name='btnLogin']
テスト手順:
- に行く https://demo.guru99.com/v1/
- このセクションでは、上記のデモ サイトを使用して、XPath のさまざまな機能を持つ要素を検索します。
AND および OR、親軸、開始文字軸、および XPath 軸を使用して要素を検索します。
AND OR の例
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class AND_OR {
public static void main(String[] args) {
WebDriver driver;
WebElement w,x;
System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe");
driver= new ChromeDriver();
// Launch the application
driver.get("https://www.guru99.com/");
//Search element using OR in the xpath
w=driver.findElement(By.xpath("//*[@type='submit' OR @name='btnReset']"));
//Print the text of the element
System.out.println(w.getText());
//Search element using AND in the xpath
x=driver.findElement(By.xpath("//input[@type='submit' and @name='btnLogin']"));
//Print the text of the searched element
System.out.println(x.getText());
//Close the browser
driver.quit();
}
}
XPath の親 Selenium
親の所属 Selenium ウェブページで現在選択されているノードの親ノードを取得するために使用されるメソッドです。要素を選択し、XPathを使用して親要素を取得する必要がある場合に非常に便利です。このメソッドは、親の親を取得するためにも使用されます。
ここで、XPathクエリは次のようになります。
Xpath=//*[@id='rt-feature']//parent::div
親を使用する XPath
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Parent {
public static void main(String[] args) {
WebDriver driver;
WebElement w;
System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe");
driver= new ChromeDriver();
// Launch the application
driver.get("https://www.guru99.com/");
//Search the element by using PARENT
w=driver.findElement(By.xpath("//*[@id='rt-feature']//parent::div"));
//Print the text of the searched element
System.out.println(w.getText());
//Close the browser
driver.quit();
}
}
で始まる
Starts-with 関数を使用すると、更新やクリック、送信などの他の操作時に属性が動的に変化する要素を見つけることができます。
ここでの XPath クエリは次のようになります
Xpath=//label[starts-with(@id,'message')]
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class StartsWith {
public static void main(String[] args) {
WebDriver driver;
WebElement w;
System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe");
driver= new ChromeDriver();
// Launch the application
driver.get("https://www.guru99.com/");
//Search the element by using starts-with
w=driver.findElement(By.xpath("//label[starts-with(@id,'message')]"));
//Print the text of the searched element
System.out.println(w.getText());
//Close the browser
driver.quit();
}
}
Xpath軸
XPath 軸を使用すると、Web ページ上の動的で非常に複雑な要素を見つけることができます。XPath 軸には、要素を見つけるためのいくつかの方法が含まれています。ここでは、いくつかの方法について説明します。
フォロー中: この関数は、特定のコンポーネントの直接要素を返します。
ここでの XPath クエリは次のようになります
Xpath=//*[@type='text']//following::input

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Following {
public static void main(String[] args) {
WebDriver driver;
WebElement w;
System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe");
driver= new ChromeDriver();
// Launch the application
driver.get("https://www.guru99.com/");
//Search the element by using Following method
w=driver.findElement(By.xpath("//*[@type='text']//following::input"));
//Print the text of the searched element
System.out.println(w.getText());
//Close the browser
driver.quit();
}
}
前: この関数は、特定の要素の前の要素を返します。
ここでの XPath クエリは次のようになります
Xpath= //*[@type='submit']//preceding::input
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Preceding {
public static void main(String[] args) {
WebDriver driver;
WebElement w;
System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe");
driver= new ChromeDriver();
// Launch the application
driver.get("https://www.guru99.com/");
//Search the element by using preceding method
w=driver.findElement(By.xpath("//*[@type='submit']//preceding::input"));
//Print the searched element
System.out.println(w.getText());
//Close the browser
driver.quit();
}
}
d) 子孫: この関数は、特定の要素の子孫要素を返します。
ここでの XPath クエリは次のようになります
Xpath= //*[@id='rt-feature']//descendant::a
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Descendant {
public static void main(String[] args) {
WebDriver driver;
WebElement w;
System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe");
driver= new ChromeDriver();
// Launch the application
driver.get("https://www.guru99.com/");
//Search the element by using descendant method
w=driver.findElement(By.xpath("//*[@id='rt-feature']//descendant::a"));
//Print the searched element
System.out.println(w.getText());
//Close the browser
driver.quit();
}
}










