XPath Şunları İçerir: Metin, Kardeş ve Ata'nın Ardından Selenium

XPath Neleri İçerir?

XPath şunları içerir: belirli bir metni içeren web öğelerini aramak için kullanılan Xpath ifadesi içindeki bir işlevdir. Web sayfası boyunca XPath include() fonksiyonunu kullanarak verilen metin değeriyle eşleşen tüm öğeleri çıkartabiliriz. XPath'ta bulunanlar, kısmi metin içeren öğeyi bulma yeteneğine sahiptir.

Örnek – metin içerir
Burada ' şeklinde bir çapa arıyoruz.SAP M'.

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

XPath İçerir

NOT: Aşağıdaki XPath alıştırmasını bu konuda uygulayabilirsiniz https://demo.guru99.com/test/selenium-xpath.html

eğer basit XPath Test betiğimiz için karmaşık bir web öğesi bulamıyorsak, XPath 1.0 kütüphanesindeki fonksiyonları kullanmamız gerekiyor. Bu fonksiyonların kombinasyonu ile daha spesifik XPath oluşturabiliriz.

XPath'de Kardeşi Takip Etme

A Kardeş Selenium Web sürücüsü ana öğenin kardeşi olan bir web öğesini getirmek için kullanılan bir işlevdir. Ana öğe biliniyorsa, Selenyum web sürücüsündeki Xpath ifadesinin kardeş özelliğini kullanabilen web öğesi kolayca bulunabilir veya konumlandırılabilir.

XPath Örneğinde Kardeş:
Burada 'a'nın kardeş elemanına dayanarak 'h4'ü buluyoruz

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

XPath'de Kardeşi Takip Etme

Ata: Ana öğe temelinde bir öğe bulmak için XPath'ın ata özelliğini kullanabiliriz.

XPath'de Kardeşi Takip Etme

Bir örnek kullanarak bu 3 işlevi anlayalım –

Test Adımları:

Not: Eğitimin oluşturulma tarihinden bu yana Guru99'un Ana Sayfası güncellendi, bu nedenle testleri çalıştırmak yerine demo sitesini kullanın

  1. MyCAD'de yazılım Güncelleme ye git https://demo.guru99.com/test/guru99home/
  2. 'En popüler kurslarımızdan birkaçı' bölümünde, metni 'SELENIUM' olan bir WebElement'in kardeşi olan tüm Web Elementlerini arayın.
  3. Ata ve kardeş işlevini içeren XPath metnini kullanarak öğeyi bulacağız.

XPath'de Kardeşi Takip Etme

KULLANMA Metin ve XPath Kardeşini İçerir

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

Çıktı şöyle olacaktır:

KULLANMA Metin ve XPath Kardeşini İçerir

XPath'ın atası Selenium

XPath'ın atası Selenium belirtilen katmandaki belirli bir üyenin atasını bulmak için kullanılan bir fonksiyondur. Döndürülecek ata seviyesi veya üyenin seviyesine göre ata seviyesi açıkça belirtilebilir. Kullanıcının istediği belirtilen ata'yı bularak ata'dan hiyerarşik adım sayısını döndürür.

Şimdi metni 'SELENIUM' olan çapanın atası yardımıyla 'Popüler kurs' bölümündeki tüm öğeleri aramamız gerektiğini varsayalım.

Burada xpath sorgumuz şöyle olacak

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

Kodu tamamla

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

Çıktı şöyle görünecek:

Kodu tamamla

VE ve VEYA'yı kullanma

AND ve OR kullanarak XPath ifademize 2 koşul koyabilirsiniz.

  • VE durumunda her iki koşul da doğru olmalıdır, o zaman yalnızca öğeyi bulur.
  • VEYA durumunda 2 koşuldan herhangi biri doğru olmalıdır, o zaman yalnızca öğeyi bulur.

Burada XPath sorgumuz şöyle olacak

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

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

VE ve VEYA'yı kullanma

Test Adımları:

  1. MyCAD'de yazılım Güncelleme ye git https://demo.guru99.com/v1/
  2. Bu bölümde, XPath'ın farklı işlevlerine sahip öğeleri aramak için yukarıdaki demo sitesini kullanacağız.

AND ve OR, parent, start-with ve XPath eksenlerini kullanan bir öğe bulacaksınız

VE VEYA Örnek

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 Ebeveyn girişi Selenium

Ebeveyn girişi Selenium web sayfasında seçilen mevcut düğümün üst düğümünü almak için kullanılan bir yöntemdir. Bir öğe seçtiğinizde ve ana öğeyi Xpath kullanarak almanız gerektiğinde çok kullanışlıdır. Bu yöntem aynı zamanda ebeveynin ebeveynini almak için de kullanılır.

Burada XPath sorgumuz şöyle olacak

Xpath=//*[@id='rt-feature']//parent::div

XPath Ebeveyn girişi Selenium

Parent'i kullanarak 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();

	}

}

İle başlar

Birlikte Başlar işlevini kullanarak, yenileme veya tıklama, gönderme vb. diğer işlemlerde özniteliği dinamik olarak değişen öğeyi bulabilirsiniz.

Burada XPath sorgumuz şöyle olacak

Xpath=//label[starts-with(@id,'message')]

İle başlar

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 eksenleri

XPath eksenlerini kullanarak, bir web sayfasındaki dinamik ve oldukça karmaşık öğeleri bulabilirsiniz. XPath eksenleri, bir öğeyi bulmak için çeşitli yöntemler içerir. Burada, birkaç yöntemi tartışacağız.

takip etme: Bu işlev, belirli bir bileşenin doğrudan öğesini döndürür.

Burada XPath sorgumuz şöyle olacak

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

Aşağıdakileri kullanarak XPath
Aşağıdakileri kullanarak 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();
	}

}

Önceki: Bu işlev, belirli bir öğenin önceki öğesini döndürür.

Burada XPath sorgumuz şöyle olacak

Xpath= //*[@type='submit']//preceding::input

Öncekini kullanarak XPath

Öncekini kullanarak 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) Azalan: Bu fonksiyon belirli bir elemanın alt elemanını döndürecektir.

Burada XPath sorgumuz şöyle olacak

Xpath= //*[@id='rt-feature']//descendant::a

Descendant'ı kullanan XPath

Descendant'ı kullanan 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();

	}

}

ÖZET

  • Bir öğeyi bulmak için normal XPath'ın kullanılamadığı bazı durumlar vardır. Böyle bir durumda xpath sorgusundan farklı fonksiyonlara ihtiyacımız var.
  • XPath'in içerdiği bazı önemli XPath fonksiyonları vardır; ebeveyn, atalar, sonraki kardeşler, vb.
  • Bu fonksiyonların yardımıyla karmaşık XPath ifadeleri oluşturabilirsiniz.