XPath में शामिल है: पाठ, निम्नलिखित भाई-बहन और पूर्वज Selenium

🚀 स्मार्ट सारांश

XPath में शामिल है, भाई-बहन, और पूर्वज Selenium संरचित संबंधों और टेक्स्ट पैटर्न का उपयोग करके सटीक वेब तत्व पहचान सक्षम करें। ये XPath फ़ंक्शन जटिल DOM पदानुक्रमों में स्वचालन विश्वसनीयता, लचीलापन और रखरखाव क्षमता को बढ़ाते हैं।

  • उपयोग contains() आंशिक पाठ के साथ गतिशील तत्वों का पता लगाने के लिए, पाठ भिन्नताओं के खिलाफ मजबूती में सुधार करना।
  • लागू करें following-sibling और ancestor तत्व पदानुक्रमों को कुशलतापूर्वक पार करने के लिए औरtracटी संबंधित नोड्स।
  • परिष्कृत और सटीक तत्व लक्ष्यीकरण के लिए फ़ंक्शन को तार्किक ऑपरेटरों (AND, OR) के साथ संयोजित करें।
  • जटिल DOM वृक्षों को व्यवस्थित रूप से नेविगेट करने के लिए XPath अक्षों (पूर्ववर्ती, वंशज) का लाभ उठाएं।
  • इन अभिव्यक्तियों को एकीकृत करें Selenium रखरखाव योग्य, गतिशील और लचीले वेब परीक्षण स्वचालन के लिए स्क्रिप्ट।

XPath में शामिल है

XPath में क्या शामिल है?

XPath में शामिल है XPath एक्सप्रेशन के भीतर एक फ़ंक्शन है, जिसका उपयोग किसी विशिष्ट टेक्स्ट वाले वेब तत्वों को खोजने के लिए किया जाता है। हम इसे इस प्रकार देख सकते हैं:tracवेबपेज पर मौजूद XPath contains() फ़ंक्शन का उपयोग करके दिए गए टेक्स्ट मान से मेल खाने वाले सभी तत्वों को खोजें। XPath में contains फ़ंक्शन आंशिक टेक्स्ट वाले तत्व को खोजने की क्षमता रखता है।

उदाहरण – इसमें पाठ शामिल है

यहां हम एक एंकर खोज रहे हैं। इसमें पाठ इस प्रकार है 'SAP एम'।

"//h4/a[contains(text(),'SAP M')]"

XPath में शामिल है

नोट: आप इस पर निम्नलिखित XPath अभ्यास का अभ्यास कर सकते हैं https://demo.guru99.com/test/selenium-xpath.html

यदि एक साधारण एक्सपाथ अगर हमें अपनी टेस्ट स्क्रिप्ट के लिए कोई जटिल वेब एलिमेंट नहीं मिल रहा है, तो हमें XPath 1.0 लाइब्रेरी के फ़ंक्शन इस्तेमाल करने होंगे। इन फ़ंक्शन के संयोजन से, हम एक ज़्यादा विशिष्ट XPath बना सकते हैं।

👉 निःशुल्क लाइव के लिए नामांकन करें Selenium परीक्षण परियोजना

XPath में भाई-बहन का अनुसरण करना

A भाई-बहन Selenium वेबड्राइवर यह एक ऐसा फ़ंक्शन है जिसका उपयोग पैरेंट एलिमेंट के सिबलिंग वेब एलिमेंट को लाने के लिए किया जाता है। यदि पैरेंट एलिमेंट ज्ञात है, तो वेब एलिमेंट को आसानी से ढूँढा या ढूँढा जा सकता है, जिसके लिए XPath एक्सप्रेशन के सिबलिंग एट्रिब्यूट का उपयोग किया जा सकता है। Selenium वेबड्राइवर.

XPath में सहोदर उदाहरण:
यहाँ, 'a' के सहोदर अवयव के आधार पर हम 'h4' ज्ञात कर रहे हैं

"//div[@class='canvas- graph']//a[@href='/accounting.html'][i[@class='icon-usd']]/following-sibling::h4"

XPath में भाई-बहन का अनुसरण करना

पूर्वज: मूल तत्व के आधार पर किसी तत्व को खोजने के लिए, हम XPath की पूर्वज विशेषता का उपयोग कर सकते हैं।

XPath में भाई-बहन का अनुसरण करना

आइए इन 3 कार्यों को एक उदाहरण से समझते हैं –

परीक्षण चरण:

नोट: ट्यूटोरियल के निर्माण की तिथि से, होमपेज Guru99 को अपडेट कर दिया गया है, इसलिए परीक्षण चलाने के लिए डेमो साइट का उपयोग करें।

  1. https://demo.guru99.com/test/guru99home/
  2. 'हमारे कुछ सर्वाधिक लोकप्रिय पाठ्यक्रम' अनुभाग में, उन सभी वेब तत्वों को खोजें जो उस वेब तत्व के सहोदर हैं जिसका पाठ 'SELENIUM' है
  3. हम XPath टेक्स्ट contains, ancestor, और sibling फ़ंक्शन का उपयोग करके तत्वों को ढूंढेंगे।

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();
        }
    }
}

आउटपुट इस प्रकार होगा:

USING में टेक्स्ट और XPath भाई-बहन शामिल हैं

XPath पूर्वज Selenium

XPath पूर्वज Selenium यह एक फ़ंक्शन है जिसका उपयोग निर्दिष्ट परत पर किसी विशिष्ट तत्व के पूर्वज को खोजने के लिए किया जाता है। लौटाए जाने वाले पूर्वज का स्तर या सदस्य के स्तर के सापेक्ष पूर्वज का स्तर स्पष्ट रूप से निर्दिष्ट किया जा सकता है। यह पूर्वज से पदानुक्रमित चरणों की संख्या लौटाता है, और उपयोगकर्ता द्वारा वांछित निर्दिष्ट पूर्वज का पता लगाता है।

अब, मान लीजिए कि हमें 'लोकप्रिय पाठ्यक्रम' अनुभाग में सभी तत्वों को उस एंकर के पूर्वज की सहायता से खोजना है जिसका टेक्स्ट 'सेलेनियम' है

यहाँ हमारी xpath क्वेरी कुछ इस प्रकार होगी

"//div[.//a[text()='SELENIUM']]/ancestor::div[@class='rt-grid-2 rt-omega']/following-sibling::div"

पूर्ण 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();
        }
    }
}

आउटपुट इस प्रकार दिखेगा-

पूर्ण Code

AND और OR का प्रयोग

AND और OR का उपयोग करके, आप हमारे XPath अभिव्यक्ति में 2 शर्तें रख सकते हैं।

  • AND के मामले में, दोनों स्थितियाँ सत्य होनी चाहिए, तभी यह तत्व ढूंढता है।
  • OR के मामले में, दो में से कोई एक शर्त सत्य होनी चाहिए, तभी यह तत्व ढूंढता है।

यहाँ, हमारी XPath क्वेरी इस प्रकार होगी

Xpath=//*[@type='submit' OR @name='btnReset']

Xpath=//input[@type='submit' and @name='btnLogin']

AND और OR का प्रयोग

परीक्षण चरण:

  1. https://demo.guru99.com/v1/
  2. इस अनुभाग में, हम XPath के विभिन्न कार्यों वाले तत्वों की खोज के लिए उपरोक्त डेमो साइट का उपयोग करेंगे।

आपको AND और OR, पैरेंट, स्टार्ट-विथ, और XPath अक्षों का उपयोग करके एक तत्व मिलेगा

और या उदाहरण

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 पैरेंट इन Selenium

पैरेंट का उपयोग करके 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 अक्षों का उपयोग करके, आप वेब पेज पर गतिशील और बहुत जटिल तत्वों को खोज सकते हैं। XPath अक्षों में तत्व खोजने के लिए कई विधियाँ होती हैं। यहाँ, कुछ विधियों पर चर्चा की जाएगी।

निम्नलिखित: यह फ़ंक्शन विशेष घटक का तत्काल तत्व लौटाएगा।

यहाँ हमारी XPath क्वेरी कुछ इस प्रकार होगी

Xpath=//*[@type='text']//following::input

XPath का उपयोग निम्नलिखित
XPath का उपयोग निम्नलिखित तरीके से करें
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

XPath का उपयोग पूर्ववर्ती

XPath का उपयोग पूर्ववर्ती
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

Descendant का उपयोग करके XPath

Descendant का उपयोग करके XPath
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();
	}
}

अक्सर पूछे जाने वाले प्रश्न

XPath में, /* किसी XML या HTML दस्तावेज़ के सभी रूट-स्तरीय तत्वों का चयन करता है। इसका अर्थ है "रूट नोड के ठीक नीचे किसी भी तत्व का चयन करें।" उदाहरण के लिए, किसी HTML दस्तावेज़ में, /* क्योंकि यह रूट के नीचे शीर्ष-स्तरीय तत्व है।

XPath में contains() फ़ंक्शन उन तत्वों का पता लगाने में मदद करता है जिनकी विशेषता या टेक्स्ट किसी दिए गए मान से आंशिक रूप से मेल खाता है। यह विशेष रूप से तब उपयोगी होता है जब सटीक स्ट्रिंग अप्रत्याशित या गतिशील हो। उदाहरण के लिए, //div[contains(@class,'menu')] किसी से मेल खाता है जिसके वर्ग में “मेनू” शब्द शामिल है।

तत्वों को उनके वर्ग विशेषता के भीतर आंशिक मिलान के आधार पर ढूँढने के लिए, contains() फ़ंक्शन का उपयोग करें। उदाहरण के लिए, //button[contains(@class,'submit')] किसी भी क्लास नाम को "सबमिट" के साथ लक्षित करता है, जैसे "सबमिट-बटन" या "फ़ॉर्म-सबमिट"। यह गतिशील क्लास नामों को संभालने का एक लचीला तरीका है।

इस पोस्ट को संक्षेप में इस प्रकार लिखें: