XPath sadrži: tekst, sljedeći brat i sestra i predak u Selenium
Što XPath sadrži?
XPath sadrži je funkcija unutar Xpath izraza koja se koristi za traženje web elemenata koji sadrže određeni tekst. Možemo izdvojiti sve elemente koji odgovaraju zadanoj vrijednosti teksta pomoću funkcije XPath contains() na cijeloj web stranici. Sadrži u XPathu ima mogućnost pronalaženja elementa s djelomičnim tekstom.
Primjer – sadrži tekst
Ovdje tražimo sidro .sadrži tekst kao 'SAP M'.
"//h4/a[contains(text(),'SAP M')]"
NAPOMENA: Na ovome možete vježbati sljedeću XPath vježbu https://demo.guru99.com/test/selenium-xpath.html
Ako je jednostavan XPath ne može pronaći komplicirani web element za našu testnu skriptu, moramo koristiti funkcije iz biblioteke XPath 1.0. Kombinacijom ovih funkcija možemo stvoriti specifičniji XPath.
Praćenje brata ili sestre u XPathu
A Brat i sestra u Selenium Webdriver je funkcija koja se koristi za dohvaćanje web elementa koji je brat nadređenog elementa. Ako je nadređeni element poznat, tada se web-element može lako pronaći ili locirati koji može koristiti srodni atribut izraza Xpath u selenium webdriveru.
Primjer brata i sestre u XPathu:
Ovdje na temelju srodnog elementa 'a' nalazimo 'h4'
"//div[@class='canvas- graph']//a[@href='/accounting.html'][i[@class='icon-usd']]/following-sibling::h4"
Predak: Za pronalaženje elementa na temelju nadređenog elementa možemo koristiti atribut pretka XPath-a.
Razumimo ove 3 funkcije koristeći primjer –
Testni koraci:
Bilješka: Od datuma kreiranja vodiča, početna stranica Guru99 je ažurirana pa umjesto toga koristite demo stranicu za pokretanje testova
- Idi na https://demo.guru99.com/test/guru99home/
- U odjeljku 'Nekoliko naših najpopularnijih tečajeva' pretražite sve Web elemente koji su srodni WebElementu čiji je tekst 'SELENIUM'
- Pronaći ćemo element pomoću XPath teksta koji sadrži ,prethodnu i srodnu funkciju.
USING Sadrži Text i XPath Sibling
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(); } }
Izlaz će biti kao:
XPath predak u Selenium
XPath predak u Selenium je funkcija koja se koristi za pronalaženje pretka određenog člana na navedenom sloju. Razina pretka koja se vraća ili razina pretka u odnosu na razinu člana može se eksplicitno specificirati. Vraća broj hijerarhijskih koraka od pretka, locirajući navedenog pretka kojeg korisnik želi.
Sada pretpostavimo da trebamo pretražiti sve elemente u odjeljku 'Popular course' uz pomoć pretka sidra čiji je tekst 'SELENIUM'
Ovdje će naš xpath upit izgledati kao
"//div[.//a[text()='SELENIUM']]/ancestor::div[@class='rt-grid-2 rt-omega']/following-sibling::div"
Kompletan kod
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(); } }
Izlaz će izgledati kao-
Korištenje I i ILI
Korištenjem AND i OR možete staviti 2 uvjeta u naš XPath izraz.
- U slučaju AND oba bi uvjeta trebala biti istinita, tada samo pronalazi element.
- U slučaju ILI bilo koji od 2 uvjeta trebao bi biti istinit, tada samo on pronalazi element.
Ovdje će naš XPath upit izgledati kao
Xpath=//*[@type='submit' OR @name='btnReset']
Xpath=//input[@type='submit' and @name='btnLogin']
Testni koraci:
- Idi na https://demo.guru99.com/v1/
- U odjeljku će koristiti gornju demo stranicu za pretraživanje elementa s različitim funkcijama XPatha.
Element ćete pronaći koristeći AND i OR, nadređene, počinje s i XPath osi
I ILI Primjer
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 roditelj u Selenium
Roditelj u Selenium je metoda koja se koristi za dohvaćanje nadređenog čvora trenutno odabranog čvora na web stranici. Vrlo je korisno u situaciji kada odaberete element i trebate dobiti nadređeni element koristeći Xpath. Ova se metoda također koristi za dobivanje roditeljskog roditelja.
Ovdje će naš XPath upit izgledati kao
Xpath=//*[@id='rt-feature']//parent::div
XPath pomoću nadređenog
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(); } }
Počinje sa
Pomoću funkcije Starts-with možete pronaći element čiji se atribut dinamički mijenja pri osvježavanju ili drugim operacijama poput klika, slanja itd.
Ovdje će naš XPath upit izgledati kao
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 osi
Korištenjem XPath osi možete pronaći dinamične i vrlo složene elemente na web stranici. XPath osi sadrže nekoliko metoda za pronalaženje elementa. Ovdje ćemo raspravljati o nekoliko metoda.
sljedeći: Ova funkcija će vratiti neposredni element određene komponente.
Ovdje će naš XPath upit izgledati kao
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(); } }
Prethodno: Ova funkcija će vratiti prethodni element određenog elementa.
Ovdje će naš XPath upit izgledati kao
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) Silazni: Ova funkcija će vratiti element potomak određenog elementa.
Ovdje će naš XPath upit izgledati kao
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(); } }
Rezime
- Postoje neke situacije kada se uobičajeni XPath ne može koristiti za pronalaženje elementa. U takvoj situaciji trebamo različite funkcije iz xpath upita.
- Postoje neke važne XPath funkcije kao što je XPath sadržava, roditelj, preci, sljedeći brat, itd.
- Uz pomoć ovih funkcija možete stvoriti složene XPath izraze. Ako trebate razumjeti kako se konkretno koristi contents u XPathu, funkcija contains in selenium pruža jednostavan način pretraživanja web elemenata na temelju djelomičnog podudaranja teksta.