XPath Şunları İçerir: Metin, Kardeş ve Ata'nın Ardından Selenium
🚀 Akıllı Özet
XPath İçerir, Kardeş ve Ata Selenium Yapılandırılmış ilişkiler ve metin kalıpları kullanarak hassas web öğesi tanımlamasını etkinleştirin. Bu XPath işlevleri, karmaşık DOM hiyerarşilerinde otomasyon güvenilirliğini, esnekliğini ve sürdürülebilirliğini artırır.
XPath Neleri İçerir?
XPath şunları içerir: XPath ifadesi içindeki bir fonksiyondur ve belirli bir metni içeren web öğelerini aramak için kullanılır. Örneğin, `for` fonksiyonunu şu şekilde tanımlayabiliriz:tracXPath'in contains() fonksiyonunu kullanarak web sayfasındaki verilen metin değeriyle eşleşen tüm öğeleri bulun. XPath'teki contains fonksiyonu, kısmi metne sahip öğeleri bulma özelliğine sahiptir.
Örnek – metin içerir
Burada ' şeklinde bir çapa arıyoruz.SAP M'.
"//h4/a[contains(text(),'SAP M')]"
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 işlevleri kullanmamız gerekiyor. Bu işlevlerin birleşimiyle daha spesifik bir XPath oluşturabiliriz.
👉 Ücretsiz Canlı Kayıt Olun Selenium Test Projesi
XPath'de Kardeşi Takip Etme
A Kardeş Selenium Web sürücüsü Üst öğenin kardeşi olan bir web öğesini getirmek için kullanılan bir işlevdir. Üst öğe biliniyorsa, web öğesi kolayca bulunabilir veya konumlandırılabilir; bunun için XPath ifadesinin kardeş niteliği kullanılabilir. Selenium WebDriver.
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"
Ata:Bir elemanı ana elemana göre bulmak için XPath'in ancestor niteliğini kullanabiliriz.
Bu 3 fonksiyonu bir örnek kullanarak anlayalım:
Test Adımları:
Not: Bu eğitimin oluşturulduğu tarihten itibaren, Ana Sayfa Guru99 numaralı sürüm güncellendi, bu nedenle testleri çalıştırmak için bunun yerine demo sitesini kullanın.
- MyCAD'de yazılım Güncelleme ye git https://demo.guru99.com/test/guru99home/
- 'En popüler kurslarımızdan birkaçı' bölümünde, metni 'SELENIUM' olan bir WebElement'in kardeşleri olan tüm Web Elementlerini arayın
- XPath metin içeren, ata ve kardeş fonksiyonlarını kullanarak elemanları bulacağız.
KULLANMA Metin ve XPath Kardeşini İçerir
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();
}
}
}
Çıktı şöyle olacaktır:
XPath'ın atası Selenium
XPath'ın atası Selenium Belirtilen katmandaki belirli bir öğenin atasını bulmak için kullanılan bir işlevdir. 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ı bulmak için ata'dan itibaren hiyerarşik adım sayısını döndürür.
Şimdi, 'Selenium' metninin atası olan çapanın yardımıyla 'Popüler ders' 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"
Tamamla 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();
}
}
}
Çıktı şöyle görünecek:
VE ve VEYA'yı kullanma
AND ve OR kullanarak XPath ifademize 2 koşul koyabiliriz.
- VE durumunda her iki koşulun da doğru olması gerekir, ancak o zaman eleman bulunur.
- VEYA durumunda 2 koşuldan herhangi biri doğru olmalıdır, ancak o zaman eleman bulunur.
Burada XPath sorgumuz şöyle olacak:
Xpath=//*[@type='submit' OR @name='btnReset']
Xpath=//input[@type='submit' and @name='btnLogin']
Test Adımları:
- MyCAD'de yazılım Güncelleme ye git https://demo.guru99.com/v1/
- Bu bölümde, yukarıdaki demo sitesini kullanarak XPath'in farklı işlevlerine sahip elemanları arayacağı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çili olan düğümün üst düğümünü almak için kullanılan bir yöntemdir. Bir öğeyi seçtiğinizde ve XPath kullanarak üst öğeyi almanız gerektiğinde oldukça kullanışlıdır. Bu yöntem, üst öğenin üst öğesini almak için de kullanılır.
Burada XPath sorgumuz şöyle olacak:
Xpath=//*[@id='rt-feature']//parent::div
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
Starts-with fonksiyonunu kullanarak, yenileme veya tıklama, gönderme vb. işlemler sırasında niteliği dinamik olarak değişen öğeyi bulabilirsiniz.
Burada XPath sorgumuz şöyle olacak
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 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

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










