XPath ประกอบด้วย: ข้อความ การติดตามพี่น้องและบรรพบุรุษใน Selenium

XPath ประกอบด้วยอะไร?

XPath ประกอบด้วย เป็นฟังก์ชันภายในนิพจน์ Xpath ซึ่งใช้เพื่อค้นหาองค์ประกอบเว็บที่มีข้อความเฉพาะ เราสามารถแยกองค์ประกอบทั้งหมดที่ตรงกับค่าข้อความที่กำหนดได้โดยใช้ฟังก์ชัน XPath contains() ทั่วทั้งหน้าเว็บ ประกอบด้วยใน XPath มีความสามารถในการค้นหาองค์ประกอบที่มีข้อความบางส่วน

ตัวอย่าง – มีข้อความ
ที่นี่เรากำลังค้นหาจุดยึดที่มีข้อความว่า 'SAP เอ็ม'

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

XPath ประกอบด้วย

หมายเหตุ: คุณสามารถฝึกฝนแบบฝึกหัด XPath ต่อไปนี้ได้ https://demo.guru99.com/test/selenium-xpath.html

ถ้าจะเรียบง่าย XPath ไม่พบองค์ประกอบเว็บที่ซับซ้อนสำหรับสคริปต์ทดสอบของเรา เราจำเป็นต้องใช้ฟังก์ชันจากไลบรารี XPath 1.0 ด้วยการผสมผสานฟังก์ชันเหล่านี้ เราสามารถสร้าง XPath ที่เฉพาะเจาะจงมากขึ้นได้

ติดตามพี่น้องใน XPath

A น้องอิน Selenium ไดรเวอร์เว็บ เป็นฟังก์ชันที่ใช้ดึงองค์ประกอบเว็บซึ่งเป็นองค์ประกอบที่เกี่ยวข้องกับองค์ประกอบหลัก หากทราบองค์ประกอบหลักแล้ว ก็สามารถค้นหาหรือระบุตำแหน่งองค์ประกอบเว็บได้โดยง่าย โดยสามารถใช้แอตทริบิวต์ที่เกี่ยวข้องกับนิพจน์ Xpath ใน Selenium webdriver ได้

พี่น้องในตัวอย่าง XPath:
บนพื้นฐานขององค์ประกอบพี่น้องของ 'a' เรากำลังค้นหา 'h4'

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

ติดตามพี่น้องใน XPath

บรรพบุรุษ: ในการค้นหาองค์ประกอบบนพื้นฐานขององค์ประกอบหลัก เราสามารถใช้แอตทริบิวต์บรรพบุรุษของ XPath

ติดตามพี่น้องใน XPath

มาทำความเข้าใจฟังก์ชันทั้ง 3 เหล่านี้โดยใช้ตัวอย่าง –

ขั้นตอนการทดสอบ:

หมายเหตุ นับตั้งแต่วันที่สร้างบทช่วยสอน หน้าแรกของ Guru99 ได้รับการอัปเดต ดังนั้นให้ใช้ไซต์สาธิตแทนเพื่อทำการทดสอบ

  1. ไปที่ https://demo.guru99.com/test/guru99home/
  2. ในส่วน 'หลักสูตรยอดนิยมบางส่วนของเรา' ให้ค้นหาองค์ประกอบเว็บทั้งหมดที่เป็นพี่น้องขององค์ประกอบเว็บที่มีข้อความว่า 'SELENIUM'
  3. เราจะค้นหาองค์ประกอบที่ใช้ข้อความ XPath ประกอบด้วยฟังก์ชันบรรพบุรุษและพี่น้อง

ติดตามพี่น้องใน XPath

การใช้ประกอบด้วยข้อความและ 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();
    }
}

ผลลัพธ์จะเป็นดังนี้:

การใช้ประกอบด้วยข้อความและ XPath Sibling

บรรพบุรุษ XPath ใน Selenium

บรรพบุรุษ XPath ใน Selenium เป็นฟังก์ชันที่ใช้ค้นหาบรรพบุรุษของสมาชิกเฉพาะในเลเยอร์ที่ระบุ โดยสามารถระบุระดับของบรรพบุรุษที่จะส่งคืนหรือระดับของบรรพบุรุษที่สัมพันธ์กับระดับของสมาชิกได้อย่างชัดเจน โดยฟังก์ชันนี้จะส่งคืนจำนวนขั้นตอนตามลำดับชั้นจากบรรพบุรุษ เพื่อระบุบรรพบุรุษที่ระบุที่ผู้ใช้ต้องการ

ตอนนี้สมมติว่าเราต้องค้นหาองค์ประกอบทั้งหมดในส่วน 'หลักสูตรยอดนิยม' ด้วยความช่วยเหลือจากบรรพบุรุษของจุดยึดที่มีข้อความว่า '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 คุณสามารถใส่ 2 เงื่อนไขในนิพจน์ XPath ของเราได้

  • ในกรณีของ AND ทั้งสองเงื่อนไขควรเป็นจริง ก็จะพบเฉพาะองค์ประกอบเท่านั้น
  • ในกรณีที่ OR เงื่อนไขใดเงื่อนไขหนึ่งจาก 2 เงื่อนไขเป็นจริง ก็จะพบเฉพาะองค์ประกอบนั้นเท่านั้น

ที่นี่แบบสอบถาม 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

XPath โดยใช้ Descendant

XPath โดยใช้ Descendant
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 ประกอบด้วย ผู้ปกครอง บรรพบุรุษ พี่น้องที่ตามมา ฯลฯ
  • ด้วยความช่วยเหลือของฟังก์ชันเหล่านี้ คุณสามารถสร้างนิพจน์ XPath ที่ซับซ้อนได้