Az XPath tartalma: Szöveg, testvér és ős követése Selenium
🚀 Intelligens összefoglaló
XPath tartalmazza a Testvér és Ős értékeket Selenium Lehetővé teszik a webes elemek pontos azonosítását strukturált kapcsolatok és szövegminták segítségével. Ezek az XPath függvények fokozzák az automatizálás megbízhatóságát, rugalmasságát és karbantarthatóságát összetett DOM hierarchiákban.

Mit tartalmaz az XPath?
Az XPath tartalmazza egy XPath kifejezésen belüli függvény, amely egy adott szöveget tartalmazó webelemek keresésére szolgál. PéldáultracAz XPath contains() függvényével keressük meg a weboldalon az adott szövegértéknek megfelelő összes elemet. Az XPath contains függvénye képes megkeresni a részleges szöveget tartalmazó elemeket.
Példa – szöveget tartalmaz
Itt egy horgonyban keresünk .contains text mint 'SAP M'.
"//h4/a[contains(text(),'SAP M')]"
MEGJEGYZÉS: A következő XPath gyakorlatot gyakorolhatja ezen https://demo.guru99.com/test/selenium-xpath.html
Ha egy egyszerű XPath nem képes bonyolult webes elemet találni a tesztszkriptünkhöz, akkor az XPath 1.0 könyvtár függvényeit kell használnunk. Ezen függvények kombinációjával egy konkrétabb XPath-ot hozhatunk létre.
👉 Regisztrálj ingyenes élő közvetítésre Selenium Tesztelési projekt
Testvér követése XPathban
A Testvér be Selenium Webdriver egy olyan függvény, amely egy olyan webes elem lekérésére szolgál, amely a szülőelem testvére. Ha a szülőelem ismert, akkor a webes elem könnyen megtalálható vagy lokalizálható, amihez az XPath kifejezés testvér attribútuma használható a következőben: Selenium WebDriver.
Testvér XPath példában:
Itt az 'a' testvéreleme alapján 'h4'-et kapunk.
"//div[@class='canvas- graph']//a[@href='/accounting.html'][i[@class='icon-usd']]/following-sibling::h4"
ŐsEgy elem szülő elem alapján történő kereséséhez használhatjuk az XPath ancestor attribútumát.
Értsük meg ezt a 3 függvényt egy példa segítségével –
Teszt lépései:
Jegyzet: Az oktatóanyag létrehozása óta a honlap GuruA 99-es verzió frissült, ezért a tesztek futtatásához inkább a demóoldalt használd.
- Tovább https://demo.guru99.com/test/guru99home/
- A „Néhány legnépszerűbb kurzusunk” részben keressen rá az összes olyan webes elemre, amely egy olyan webes elem testvére, amelynek szövege „SZELÉNIUM”.
- XPath text contains, ancestor és sibling függvényekkel fogunk elemeket keresni.
A HASZNÁLAT szöveget és XPath testvért tartalmaz
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();
}
}
}
A kimenet a következő lesz:
XPath Ancestor be Selenium
XPath Ancestor be Selenium egy olyan függvény, amely egy adott elem ősének megkeresésére szolgál a megadott rétegen. Az ős visszaadandó szintje, vagy az ős szintje a tag szintjéhez képest explicit módon megadható. Visszaadja az őstől számított hierarchikus lépések számát, megkeresve a felhasználó által kívánt megadott őst.
Tegyük fel, hogy a „Népszerű kurzus” szakasz összes elemét kell keresnünk a horgony ősének segítségével, amelynek szövege „SZELÉNIUM”.
Itt az xpath lekérdezésünk ilyen lesz
"//div[.//a[text()='SELENIUM']]/ancestor::div[@class='rt-grid-2 rt-omega']/following-sibling::div"
teljes Code
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();
}
}
}
A kimenet így fog kinézni:
Az ÉS és a VAGY használatával
Az ÉS és VAGY használatával két feltételt adhatunk meg az XPath kifejezésben.
- ÉS esetén mindkét feltételnek igaznak kell lennie, csak akkor találja meg az elemet.
- VAGY esetén a két feltétel bármelyikének igaznak kell lennie, csak akkor találja meg az elemet.
Itt az XPath lekérdezésünk a következő lesz:
Xpath=//*[@type='submit' OR @name='btnReset']
Xpath=//input[@type='submit' and @name='btnLogin']
Teszt lépései:
- Tovább https://demo.guru99.com/v1/
- Ebben a szakaszban a fenti demó webhelyet fogjuk használni az XPath különböző függvényeivel rendelkező elemek keresésére.
Az AND és OR, szülő, starts-with és XPath tengelyek használatával találhat egy elemet
ÉS VAGY példa
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 szülő be Selenium
Szülő be Selenium egy olyan metódus, amely a weboldalon aktuálisan kiválasztott csomópont szülőcsomópontjának lekérésére szolgál. Nagyon hasznos olyan helyzetekben, amikor kiválasztunk egy elemet, és XPath használatával kell lekérdeznünk a szülőelemet. Ez a metódus a szülő szülőjének lekérésére is szolgál.
Itt az XPath lekérdezésünk a következő lesz:
Xpath=//*[@id='rt-feature']//parent::div
XPath a szülő használatával
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();
}
}
Ezzel kezdődik
A Starts-with függvénnyel megkeresheted azt az elemet, amelynek attribútuma dinamikusan változik frissítéskor vagy más műveletek, például kattintás, elküldés stb. hatására.
Itt lesz az XPath lekérdezésünk
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 tengelyek
XPath tengelyek használatával megtalálhatja a dinamikus és nagyon összetett elemeket egy weboldalon. Az XPath tengelyek számos módszert tartalmaznak egy elem megkeresésére. Itt néhány módszerről lesz szó.
következő: Ez a függvény az adott komponens azonnali elemét adja vissza.
Itt lesz az XPath lekérdezésünk
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();
}
}
Megelőző: Ez a függvény az adott elem előző elemét adja vissza.
Itt lesz az XPath lekérdezésünk
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) Csökkenő: Ez a függvény az adott elem leszármazott elemét adja vissza.
Itt lesz az XPath lekérdezésünk
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();
}
}









