XPath sisältää: tekstiä, seuraavan sisaruksen ja esivanhemman Selenium

Mitä XPath sisältää?

XPath sisältää on Xpath-lausekkeen funktio, jota käytetään etsimään tietyn tekstin sisältäviä verkkoelementtejä. Voimme poimia kaikki elementit, jotka vastaavat annettua tekstiarvoa XPath include() -funktiolla kautta koko verkkosivun. Sisältää XPathissa kyky löytää elementin osittaisella tekstillä.

Esimerkki – sisältää tekstiä
Täällä etsimme ankkuria .contains text as 'SAP M'.

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

XPath sisältää

HUOMAA: Voit harjoitella seuraavaa XPath-harjoitusta tällä https://demo.guru99.com/test/selenium-xpath.html

Jos yksinkertainen XPath ei löydä monimutkaista verkkoelementtiä testiskriptillemme, meidän on käytettävä XPath 1.0 -kirjaston toimintoja. Näiden toimintojen yhdistelmällä voimme luoda tarkempia XPath-funktioita.

Sisaruksen seuraaminen XPathissa

A Sisarus sisään Selenium Web-ohjain on funktio, jota käytetään noutamaan verkkoelementti, joka on emoelementin sisarus. Jos pääelementti tunnetaan, web-elementti voidaan helposti löytää tai paikantaa, joka voi käyttää Xpath-lausekkeen sibling-attribuuttia seleeni-webdriverissä.

Sisarus XPath-esimerkissä:
Täältä löydämme 'a':n sisaruselementin perusteella 'h4'

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

Sisaruksen seuraaminen XPathissa

kantaisä: Löytääksesi elementin emoelementin perusteella voimme käyttää XPathin ancestor-attribuuttia.

Sisaruksen seuraaminen XPathissa

Ymmärretään nämä 3 funktiota esimerkin avulla -

Testivaiheet:

Huomautus: Opetusohjelman luomispäivästä lähtien Guru99:n kotisivua on päivitetty, joten käytä demosivustoa sen sijaan testien suorittamiseen

  1. Mene https://demo.guru99.com/test/guru99home/
  2. Etsi osiosta Muutama suosituimmista kursseistamme kaikki Web-elementit, jotka ovat WebElementin sisaruksia, joiden teksti on "SELENIUM".
  3. Löydämme elementin käyttämällä XPath-tekstiä, joka sisältää ,esi- ja sisarusfunktion.

Sisaruksen seuraaminen XPathissa

KÄYTTÖ Sisältää tekstin ja XPath-sisaruksen

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

Lähtö tulee olemaan seuraavanlainen:

KÄYTTÖ Sisältää tekstin ja XPath-sisaruksen

XPath Ancestor sisään Selenium

XPath Ancestor sisään Selenium on funktio, jota käytetään etsimään tietyn jäsenen esi-isä määritetyltä tasolta. Palautettavan esi-isän taso tai esivanhemman taso suhteessa jäsenen tasoon voidaan määrittää nimenomaisesti. Se palauttaa hierarkkisten vaiheiden määrän esi-isästä ja paikantaa määritetyn esi-isän, jonka käyttäjä haluaa.

Oletetaan nyt, että meidän on etsittävä kaikki elementit "Suosittu kurssi" -osiossa ankkurin esi-isän avulla, jonka teksti on "SELENIUM"

Tässä on xpath-kyselymme

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

Täydellinen koodi

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

Tulos näyttää tältä -

Täydellinen koodi

AND- ja OR-komentojen käyttäminen

Käyttämällä AND- ja OR-toimintoja voit asettaa XPath-lauseeeseemme 2 ehtoa.

  • AND:n tapauksessa molempien ehtojen tulee olla tosi, silloin vain se löytää elementin.
  • OR:n tapauksessa jonkin kahdesta ehdosta pitäisi olla tosi, silloin vain se löytää elementin.

Tässä on XPath-kyselymme

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

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

AND- ja OR-komentojen käyttäminen

Testivaiheet:

  1. Mene https://demo.guru99.com/v1/
  2. Osassa käyttää yllä olevaa demosivustoa etsiäksesi elementtiä, jolla on XPathin eri toimintoja.

Löydät elementin käyttämällä AND- ja OR-, vanhempi-, alkaa- ja XPath-akseleita

JA TAI Esimerkki

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 Parent sisään Selenium

Vanhempi sisään Selenium on menetelmä, jota käytetään verkkosivulla valitun nykyisen solmun pääsolmun noutamiseen. Se on erittäin hyödyllinen tilanteessa, jossa valitset elementin ja haluat saada pääelementin Xpathilla. Tätä menetelmää käytetään myös vanhemman vanhemman saamiseen.

Tässä on XPath-kyselymme

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

XPath Parent sisään Selenium

XPath käyttäen Parent

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

	}

}

Alkaa

Aloita-toiminnolla voit löytää elementin, jonka attribuutti muuttuu dynaamisesti päivityksen tai muiden toimintojen, kuten napsautus, lähetys jne., yhteydessä.

Tässä on XPath-kyselymme

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

Alkaa

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-akselit

XPath-akseleiden avulla voit löytää web-sivun dynaamiset ja erittäin monimutkaiset elementit. XPath-akselit sisältävät useita tapoja löytää elementti. Tässä keskustellaan muutamasta menetelmästä.

jälkeen: Tämä toiminto palauttaa tietyn komponentin välittömän elementin.

Tässä on XPath-kyselymme

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

XPath seuraamalla
XPath käyttämällä seuraavaa
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();
	}

}

Edellinen: Tämä funktio palauttaa tietyn elementin edeltävän elementin.

Tässä on XPath-kyselymme

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

XPath käyttämällä Precedingiä

XPath käyttämällä Precedingiä
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) Laskeva: Tämä funktio palauttaa tietyn elementin jälkeläisen elementin.

Tässä on XPath-kyselymme

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

XPath käyttäen Descendantia

XPath käyttäen Descendantia
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();

	}

}

Yhteenveto

  • Joissakin tilanteissa elementtiä ei voida löytää tavallisella XPathilla. Tällaisessa tilanteessa tarvitsemme erilaisia ​​toimintoja xpath-kyselystä.
  • Siellä on joitain tärkeitä XPath-toimintoja, kuten XPath sisältää, vanhempi, esi-isät, seuraavat sisarukset jne.
  • Näiden funktioiden avulla voit luoda monimutkaisia ​​XPath-lausekkeita. Jos sinun on ymmärrettävä, miten XPathissa käytetään Sisältöä, sisältää seleenissä -toiminto tarjoaa helpon tavan etsiä verkkoelementtejä osittaisen tekstin osuman perusteella.