XPath sisaldab: teksti, järgnevat õde-venda ja esivanemat Selenium
🚀 Nutikas kokkuvõte
XPath sisaldab atribuute „Sibling” ja „Ancestor” Selenium Võimaldavad struktureeritud seoste ja tekstimustrite abil täpset veebielementide tuvastamist. Need XPath-funktsioonid suurendavad automatiseerimise usaldusväärsust, paindlikkust ja hooldatavust keerukates DOM-hierarhiates.
Mida XPath sisaldab?
XPath sisaldab on XPath-avaldise sees olev funktsioon, mida kasutatakse konkreetset teksti sisaldavate veebielementide otsimiseks. Me saame näitekstracKõikide antud tekstiväärtusele vastavate elementide leidmiseks kasutage XPath funktsiooni contains() kogu veebilehel. XPath funktsioonil Contains on võimalus leida osalise tekstiga element.
Näide – sisaldab teksti
Siin otsime ankrut .contains text as 'SAP M'.
"//h4/a[contains(text(),'SAP M')]"
MÄRKUS. Selle kohta saate harjutada järgmist XPathi harjutust https://demo.guru99.com/test/selenium-xpath.html
Kui lihtne XPath ei leia meie testskripti jaoks keerulist veebielementi, peame kasutama XPath 1.0 teegi funktsioone. Nende funktsioonide kombinatsiooni abil saame luua spetsiifilisema XPathi.
👉 Registreeru tasuta otseülekande saamiseks Selenium Testimisprojekt
Õdede-vendade jälgimine XPathis
A Õde-vend sisse Selenium Veebidraiver on funktsioon, mida kasutatakse veebielemendi toomiseks, mis on vanemelemendi õde. Kui vanemelement on teada, saab veebielementi hõlpsalt leida või leida, kasutades XPath-avaldise õe atribuuti Selenium Veebidraiver.
Vend XPathi näites:
Siin leiame 'a' õde-elemendi põhjal 'h4'
"//div[@class='canvas- graph']//a[@href='/accounting.html'][i[@class='icon-usd']]/following-sibling::h4"
EsivanemElemendi leidmiseks ülemelemendi põhjal saame kasutada XPathi atribuuti ancestor.
Saame neist kolmest funktsioonist aru näite abil –
Testi sammud:
Märge: Alates õpetuse loomise kuupäevast on avaleht Guru99 on uuendatud, seega kasutage testide tegemiseks hoopis demosaiti
- Minna https://demo.guru99.com/test/guru99home/
- Otsi jaotisest „Mõned meie populaarseimad kursused” kõiki veebielemente, mis on samanimelise veebielemendi õed-vennad, mille tekst on „SELENIUM”.
- Leiame elemente XPathi funktsioonide text contains, ancestor ja sibling abil.
KASUTAMINE Sisaldab teksti ja XPathi õde
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();
}
}
}
Väljund on selline:
XPath Ancestor sisse Selenium
XPath Ancestor sisse Selenium on funktsioon, mida kasutatakse konkreetse elemendi eellase leidmiseks määratud kihilt. Tagastatava eellase taseme või eellase taseme liikme taseme suhtes saab selgesõnaliselt määrata. See tagastab eellasest hierarhiliste sammude arvu, leides kasutaja soovitud eellase.
Oletame nüüd, et peame otsima kõiki elemente jaotises „Populaarne kursus“, kasutades ankru eelkäijat, mille tekst on „SELEENIUM“.
Siin on meie xpath päring selline
"//div[.//a[text()='SELENIUM']]/ancestor::div[@class='rt-grid-2 rt-omega']/following-sibling::div"
täielik 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();
}
}
}
Väljund näeb välja selline -
Kasutades AND ja OR
Kasutades operaatoreid AND ja OR, saate meie XPath-avaldisesse lisada kaks tingimust.
- JA korral peavad mõlemad kaks tingimust olema tõesed, ainult siis leiab see elemendi.
- VÕI korral peab üks kahest tingimusest olema tõene, ainult siis leiab see elemendi.
Siin näeb meie XPath päring välja selline
Xpath=//*[@type='submit' OR @name='btnReset']
Xpath=//input[@type='submit' and @name='btnLogin']
Testi sammud:
- Minna https://demo.guru99.com/v1/
- Selles jaotises kasutame ülaltoodud demosaiti XPathi erinevate funktsioonidega elementide otsimiseks.
Leiate elemendi, kasutades AND- ja OR-, ema-, algus- ja XPath-telge
JA VÕI Näide
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 Parent sees Selenium
Lapsevanem sisse Selenium on meetod, mida kasutatakse veebilehel valitud sõlme ülemsõlme leidmiseks. See on väga kasulik olukordades, kus valite elemendi ja peate ülemelemendi hankima XPathi abil. Seda meetodit kasutatakse ka ülemelemendi ülemelemendi leidmiseks.
Siin näeb meie XPath päring välja selline
Xpath=//*[@id='rt-feature']//parent::div
XPath, kasutades Parent
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();
}
}
Algab-koos
Funktsiooni Starts-with abil saate leida elemendi, mille atribuut värskendamisel või muude toimingute (nt klõpsamine, esitamine jne) korral dünaamiliselt muutub.
Siin on meie XPathi päring selline
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 teljed
XPathi telgede abil leiate veebilehelt dünaamilisi ja väga keerukaid elemente. XPath-teljed sisaldavad elemendi leidmiseks mitmeid meetodeid. Siin käsitleme mõnda meetodit.
Järel: see funktsioon tagastab konkreetse komponendi vahetu elemendi.
Siin on meie XPathi päring selline
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();
}
}
Eelnev: See funktsioon tagastab konkreetse elemendi eelneva elemendi.
Siin on meie XPathi päring selline
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) Langevalt: See funktsioon tagastab konkreetse elemendi järeltulija elemendi.
Siin on meie XPathi päring selline
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();
}
}










