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

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

XPath में शामिल है Xpath अभिव्यक्ति के भीतर एक फ़ंक्शन है जिसका उपयोग किसी विशेष टेक्स्ट वाले वेब तत्वों को खोजने के लिए किया जाता है। हम 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 बना सकते हैं।

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

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

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

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

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

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

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

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

परीक्षण चरण:

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

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

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

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

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class SiblingAndParentInXpath {

    @Test

    public void testSiblingAndParentInXpath(){

    	WebDriver driver;
    	String driverPath = "C:\\geckodriver.exe";
    	System.setProperty("webdriver.gecko.driver", driverPath);
        driver = new FirefoxDriver();        
        
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://demo.guru99.com/test/guru99home/");

        //Search element inside 'Popular course' which are sibling of control 'SELENIUM' ,Here first we will find a h2 whose text is ''A few of our most popular courses' ,then we move to its parent element which is a 'div' , inside this div we will find a link whose text is 'SELENIUM' then at last we will find all of the sibling elements of this link('SELENIUM')
        
        List <WebElement> dateBox = driver.findElements(By.xpath("//h2[contains(text(),'A few of our most popular courses')]/parent::div//div[//a[text()='SELENIUM']]/following-sibling::div[@class='rt-grid-2 rt-omega']"));

        //Print all the which are sibling of the the element named as 'SELENIUM' in 'Popular course'
        for (WebElement webElement : dateBox) {
            System.out.println(webElement.getText());
        }     

        driver.close();
    }
}

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

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

XPath पूर्वज Selenium

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

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

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

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

पूरा कोड

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class AncestorInXpath{

@Test

    public void testAncestorInXpath(){

        WebDriver driver = new FirefoxDriver();             
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://demo.guru99.com/test/guru99home/");

        //Search All elements in 'Popular course' section 
		//with the help of ancestor of the anchor whose text is 'SELENIUM'

        List <WebElement> dateBox = driver.findElements(By.xpath("//div[.//a[text()='SELENIUM']]/ancestor::div[@class='rt-grid-2 rt-omega']/following-sibling::div"));

        //Print all the which are sibling of the element named as 'SELENIUM' in 'Popular course'

        for (WebElement webElement : dateBox) {
            System.out.println(webElement.getText());
        }
     
        driver.quit();
    }
}

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

पूरा कोड

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 का उपयोग नहीं किया जा सकता। ऐसी स्थिति में, हमें xpath क्वेरी से अलग फ़ंक्शन की आवश्यकता होती है।
  • इसमें कुछ महत्वपूर्ण XPath फ़ंक्शन हैं जैसे XPath contains, parent, descendants, following-sibling, आदि।
  • इन फ़ंक्शन की सहायता से, आप जटिल XPath अभिव्यक्तियाँ बना सकते हैं। यदि आपको यह समझने की आवश्यकता है कि XPath में contains का उपयोग कैसे किया जाता है, तो सेलेनियम फ़ंक्शन में contains आंशिक टेक्स्ट मिलान के आधार पर वेब तत्वों को खोजने का एक आसान तरीका प्रदान करता है।