XPath sadrži: tekst, sljedeći brat i sestra i predak u Selenium
🚀 Pametni sažetak
XPath sadrži, brata/sestru i predaka u Selenium omogućuju preciznu identifikaciju web elemenata pomoću strukturiranih odnosa i tekstualnih uzoraka. Ove XPath funkcije poboljšavaju pouzdanost, fleksibilnost i održivost automatizacije u složenim DOM hijerarhijama.
Što XPath sadrži?
XPath sadrži je funkcija unutar XPath izraza koja se koristi za pretraživanje web elemenata koji sadrže određeni tekst. Možemo npr.tracsve elemente koji odgovaraju zadanoj tekstualnoj vrijednosti pomoću XPath funkcije contains() na cijeloj web stranici. Contains u XPath-u ima mogućnost pronalaska elementa s djelomičnim tekstom.
Primjer – sadrži tekst
Ovdje tražimo sidro .sadrži tekst kao 'SAP M'.
"//h4/a[contains(text(),'SAP M')]"
NAPOMENA: Na ovome možete vježbati sljedeću XPath vježbu https://demo.guru99.com/test/selenium-xpath.html
Ako je jednostavan XPath ne može pronaći komplicirani web element za naš testni skript, moramo koristiti funkcije iz XPath 1.0 biblioteke. Kombinacijom ovih funkcija možemo stvoriti specifičniji XPath.
👉 Prijavite se za besplatni prijenos uživo Selenium Projekt testiranja
Praćenje brata ili sestre u XPathu
A Brat i sestra u Selenium Webdriver je funkcija koja se koristi za dohvaćanje web elementa koji je rođak roditeljskog elementa. Ako je roditeljski element poznat, tada se web element može lako pronaći ili locirati, što se može učiniti korištenjem atributa rođaka XPath izraza u Selenium WebDriver.
Primjer brata i sestre u XPathu:
Ovdje, na temelju srodnog elementa 'a', nalazimo 'h4'
"//div[@class='canvas- graph']//a[@href='/accounting.html'][i[@class='icon-usd']]/following-sibling::h4"
PredakZa pronalazak elementa na temelju roditeljskog elementa možemo koristiti atribut predaka XPath-a.
Razumijemo ove 3 funkcije koristeći primjer –
Testni koraci:
Bilješka: Od datuma izrade tutorijala, početna stranica GuruVerzija 99 je ažurirana, stoga za provođenje testova koristite demo stranicu.
- Idi na https://demo.guru99.com/test/guru99home/
- U odjeljku 'Neki od naših najpopularnijih tečajeva' pretražite sve web elemente koji su srodni web elementu čiji je tekst 'SELENIUM'
- Elemente ćemo pronaći koristeći XPath funkcije `text contains`, `prethodnik` i `sibling`.
USING Sadrži Text i XPath Sibling
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();
}
}
}
Izlaz će biti kao:
XPath predak u Selenium
XPath predak u Selenium je funkcija koja se koristi za pronalaženje pretka određenog elementa na navedenom sloju. Razina pretka koja se vraća ili razina pretka u odnosu na razinu člana može se eksplicitno odrediti. Vraća broj hijerarhijskih koraka od pretka, locirajući određenog pretka kojeg korisnik želi.
Sada, pretpostavimo da trebamo pretražiti sve elemente u odjeljku 'Popularni tečaj' uz pomoć pretka sidra čiji je tekst 'SELENIUM'
Ovdje će naš xpath upit izgledati kao
"//div[.//a[text()='SELENIUM']]/ancestor::div[@class='rt-grid-2 rt-omega']/following-sibling::div"
potpun 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();
}
}
}
Izlaz će izgledati kao-
Korištenje I i ILI
Korištenjem operatora AND i OR možete staviti 2 uvjeta u naš XPath izraz.
- U slučaju I, oba uvjeta moraju biti istinita, tek tada pronalazi element.
- U slučaju ILI, bilo koji od 2 uvjeta mora biti istinit, samo tada pronalazi element.
Ovdje će naš XPath upit biti ovakav
Xpath=//*[@type='submit' OR @name='btnReset']
Xpath=//input[@type='submit' and @name='btnLogin']
Testni koraci:
- Idi na https://demo.guru99.com/v1/
- U ovom odjeljku koristit ćemo gornju demo stranicu za pretraživanje elemenata s različitim funkcijama XPath-a.
Element ćete pronaći koristeći AND i OR, nadređene, počinje s i XPath osi
I ILI Primjer
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 roditelj u Selenium
Roditelj u Selenium je metoda koja se koristi za dohvaćanje nadređenog čvora trenutno odabranog čvora na web stranici. Vrlo je korisna u situacijama kada odaberete element i trebate dobiti nadređeni element pomoću XPath-a. Ova se metoda također koristi za dohvaćanje nadređenog čvora.
Ovdje će naš XPath upit biti ovakav
Xpath=//*[@id='rt-feature']//parent::div
XPath pomoću nadređenog
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();
}
}
Počinje sa
Pomoću funkcije Starts-with možete pronaći element čiji se atribut dinamički mijenja pri osvježavanju ili drugim operacijama poput klika, slanja itd.
Ovdje će naš XPath upit izgledati kao
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 osi
Korištenjem XPath osi možete pronaći dinamične i vrlo složene elemente na web stranici. XPath osi sadrže nekoliko metoda za pronalaženje elementa. Ovdje ćemo raspravljati o nekoliko metoda.
sljedeći: Ova funkcija će vratiti neposredni element određene komponente.
Ovdje će naš XPath upit izgledati kao
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();
}
}
Prethodno: Ova funkcija će vratiti prethodni element određenog elementa.
Ovdje će naš XPath upit izgledati kao
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) Silazni: Ova funkcija će vratiti element potomak određenog elementa.
Ovdje će naš XPath upit izgledati kao
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();
}
}










