XPath sisältää: tekstiä, seuraavan sisaruksen ja esivanhemman Selenium
🚀 Älykäs yhteenveto
XPath sisältää merkit Sibling ja Ancestor Selenium mahdollistavat tarkan verkkoelementtien tunnistamisen käyttämällä jäsenneltyjä suhteita ja tekstimalleja. Nämä XPath-funktiot parantavat automaation luotettavuutta, joustavuutta ja ylläpidettävyyttä monimutkaisissa DOM-hierarkioissa.

Mitä XPath sisältää?
XPath sisältää on XPath-lausekkeen sisällä oleva funktio, jota käytetään tietyn tekstin sisältävien verkkoelementtien etsimiseen. Voimme esimerkiksitracEtsii kaikki annettua tekstiarvoa vastaavat elementit koko verkkosivulta XPath contains()-funktiolla. XPathissa Contains-funktiolla on kyky löytää elementti, jossa on osittainen teksti.
Esimerkki – sisältää tekstiä
Täällä etsimme ankkuria .contains text as 'SAP M'.
"//h4/a[contains(text(),'SAP M')]"
HUOMAA: Voit harjoitella seuraavaa XPath-harjoitusta tällä https://demo.guru99.com/test/selenium-xpath.html
Jos yksinkertainen XPath ei löydä monimutkaista verkkoelementtiä testiskriptillemme, meidän on käytettävä XPath 1.0 -kirjaston funktioita. Näiden funktioiden yhdistelmällä voimme luoda tarkemman XPath-lausekkeen.
👉 Ilmoittaudu ilmaiseen live-lähetykseen Selenium Testausprojekti
Sisaruksen seuraaminen XPathissa
A Sisarus sisään Selenium Web-ohjain on funktio, jota käytetään hakemaan verkkoelementti, joka on pääelementin sisarus. Jos pääelementti tunnetaan, verkkoelementti on helppo löytää tai paikantaa, mikä voi tapahtua XPath-lausekkeen sisarusattribuutin avulla. Selenium WebDriver.
Sisarus XPath-esimerkissä:
Tässä 'a':n sisaruselementin perusteella löydämme 'h4':n
"//div[@class='canvas- graph']//a[@href='/accounting.html'][i[@class='icon-usd']]/following-sibling::h4"
kantaisäLöytääksemme elementin pääelementin perusteella, voimme käyttää XPath-lausekkeen ancestor-attribuuttia.
Ymmärretään nämä 3 funktiota esimerkin avulla –
Testivaiheet:
Huomautus: Opastusohjelman luomispäivästä lähtien kotisivu Guru99 on päivitetty, joten käytä testien suorittamiseen demosivustoa.
- Mene https://demo.guru99.com/test/guru99home/
- Hae osiosta 'Muutamia suosituimpia kurssejamme' kaikki verkkoelementit, jotka ovat sellaisen verkkoelementin sisaruksia, jonka teksti on 'SELENIUM'.
- Etsimme elementtejä XPath-funktioiden text contains, ancestor ja sibling avulla.
KÄYTTÖ Sisältää tekstin ja XPath-sisaruksen
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();
}
}
}
Lähtö tulee olemaan seuraavanlainen:
XPath Ancestor sisään Selenium
XPath Ancestor sisään Selenium on funktio, jota käytetään tietyn elementin esi-isän löytämiseen määritetyltä tasolta. Palautettavan esi-isän taso tai esi-isän taso suhteessa jäsenen tasoon voidaan määrittää eksplisiittisesti. Se palauttaa hierarkkisten askelten määrän esi-isästä paikantaen käyttäjän haluaman määritetyn esi-isän.
Oletetaan nyt, että meidän on haettava kaikkia 'Suosittu kurssi' -osion elementtejä käyttämällä ankkurin esi-isää, jonka teksti on 'SELENIUM'.
Tässä on xpath-kyselymme
"//div[.//a[text()='SELENIUM']]/ancestor::div[@class='rt-grid-2 rt-omega']/following-sibling::div"
Täydellinen 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();
}
}
}
Tulos näyttää tältä -
AND- ja OR-komentojen käyttäminen
Käyttämällä JA- ja TAI-operaattoreita voit lisätä XPath-lausekkeeseemme kaksi ehtoa.
- JA-operaattorin tapauksessa molempien ehtojen tulee olla tosia, jolloin vain tämä löytää alkion.
- TAI-operaattorin tapauksessa jommankumman ehdoista on oltava tosi, jolloin se löytää alkion.
Tässä XPath-kyselymme näyttää tältä
Xpath=//*[@type='submit' OR @name='btnReset']
Xpath=//input[@type='submit' and @name='btnLogin']
Testivaiheet:
- Mene https://demo.guru99.com/v1/
- Tässä osiossa käytämme yllä olevaa demosivustoa etsiäksemme elementtejä, joilla on XPath:n eri funktioita.
Löydät elementin käyttämällä AND- ja OR-, vanhempi-, alkaa- ja XPath-akseleita
JA TAI Esimerkki
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 sisään Selenium
Vanhempi sisään Selenium on menetelmä, jota käytetään verkkosivulla valitun solmun pääsolmun hakemiseen. Se on erittäin hyödyllinen tilanteissa, joissa valitset elementin ja sinun on haettava pääelementti XPath-lausekkeen avulla. Tätä menetelmää käytetään myös pääelementin pääelementin hakemiseen.
Tässä XPath-kyselymme näyttää tältä
Xpath=//*[@id='rt-feature']//parent::div
XPath käyttäen 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();
}
}
Alkaa
Starts-with-funktion avulla voit löytää elementin, jonka ominaisuus muuttuu dynaamisesti päivityksen tai muiden toimintojen, kuten napsautuksen, lähetyksen jne., yhteydessä.
Tässä on XPath-kyselymme
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-akselit
XPath-akseleiden avulla voit löytää web-sivun dynaamiset ja erittäin monimutkaiset elementit. XPath-akselit sisältävät useita tapoja löytää elementti. Tässä keskustellaan muutamasta menetelmästä.
jälkeen: Tämä toiminto palauttaa tietyn komponentin välittömän elementin.
Tässä on XPath-kyselymme
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();
}
}
Edellinen: Tämä funktio palauttaa tietyn elementin edeltävän elementin.
Tässä on XPath-kyselymme
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) Laskeva: Tämä funktio palauttaa tietyn elementin jälkeläisen elementin.
Tässä on XPath-kyselymme
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();
}
}









