chứa [] trong Selenium là một hàm trong biểu thức Xpath được sử dụng để tìm kiếm các thành phần web có chứa một văn bản cụ thể. Chúng ta có thể trích xuất tất cả các phần tử khớp với giá trị văn bản đã cho bằng cách sử dụng hàm XPath contains[] trong toàn bộ trang web. Chứa trong XPath có khả năng tìm phần tử có một phần văn bản
Bán tại. Ở đây chúng tôi đang tìm kiếm một mỏ neo. chứa văn bản là 'SAP M'.
"//h4/a[contains[text[],'SAP M']]"
GHI CHÚ. Bạn có thể thực hành bài tập XPath sau trên
"//div[@class='canvas- graph']//a[@href='/accounting.html'][i[@class='icon-usd']]/following-sibling::h4"8 này
Anh chị em trong Selenium Webdriver là gì?
Anh chị em trong Selenium Webdriver là một chức năng được sử dụng để tìm nạp một phần tử web là anh chị em của phần tử cha. Nếu phần tử cha được biết thì có thể dễ dàng tìm thấy hoặc định vị phần tử web có thể sử dụng thuộc tính anh chị em của biểu thức Xpath trong trình quản trị web Selenium
Anh chị em trong Ví dụ XPath. Ở đây, trên cơ sở phần tử anh em của 'a', chúng tôi đang tìm 'h4'
"//div[@class='canvas- graph']//a[@href='/accounting.html'][i[@class='icon-usd']]/following-sibling::h4"
tổ tiên. Để tìm một phần tử trên cơ sở phần tử cha, chúng ta có thể sử dụng thuộc tính tổ tiên của XPath
Hãy hiểu 3 chức năng này bằng một ví dụ –
Các bước kiểm tra
Ghi chú. Kể từ ngày tạo hướng dẫn, Trang chủ của Guru99 đã được cập nhật, vì vậy hãy sử dụng trang demo để chạy thử nghiệm thay vì
- Tới
"//div[@class='canvas- graph']//a[@href='/accounting.html'][i[@class='icon-usd']]/following-sibling::h4"
9 - Trong phần 'Một số khóa học phổ biến nhất của chúng tôi', hãy tìm kiếm tất cả các Thành phần Web là anh em của một Thành phần Web có văn bản là 'SELENIUM'
- Chúng ta sẽ tìm phần tử bằng cách sử dụng văn bản XPath chứa hàm tổ tiên và anh chị em
SỬ DỤNG Chứa và 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["//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 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[]; } }
Đầu ra sẽ như thế nào
Tổ tiên trong Selenium Webdriver
Tổ tiên trong Selenium Webdriver là một chức năng được sử dụng để tìm tổ tiên của một thành viên cụ thể tại lớp được chỉ định. Cấp độ của tổ tiên được trả về hoặc cấp độ của tổ tiên so với cấp độ của thành viên có thể được chỉ định rõ ràng. Nó trả về số bước phân cấp từ tổ tiên, định vị tổ tiên được chỉ định mà người dùng muốn
Bây giờ, giả sử chúng ta cần Tìm kiếm Tất cả các yếu tố trong phần 'Khóa học phổ biến' với sự trợ giúp của tổ tiên của mỏ neo có văn bản là 'SELENIUM'
Ở đây truy vấn xpath của chúng tôi sẽ như thế nào
"//div[.//a[text[]='SELENIUM']]/ancestor::div[@class='rt-grid-2 rt-omega']/following-sibling::div"
Hoàn thành mã
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["//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 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[]; } }
Đầu ra sẽ giống như-
Sử dụng VÀ và HOẶC
Bằng cách sử dụng AND và OR, bạn có thể đặt 2 điều kiện trong biểu thức XPath của chúng tôi
- Trong trường hợp AND cả 2 điều kiện đều đúng thì chỉ nó mới tìm thấy phần tử
- Trong trường hợp OR, bất kỳ một trong 2 điều kiện nào cũng phải đúng thì chỉ nó mới tìm thấy phần tử
Ở đây truy vấn XPath của chúng tôi sẽ giống như
Xpath=//*[@type='submit' OR @name='btnReset']
Xpath=//input[@type='submit' and @name='btnLogin']
Các bước kiểm tra
- Tới
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["//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 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[]; } }
0 - Trong phần sẽ sử dụng trang web demo trên để tìm kiếm phần tử với các chức năng khác nhau của XPath
Bạn sẽ tìm thấy một phần tử bằng cách sử dụng các trục AND và OR, cha mẹ, bắt đầu bằng và XPath
VÀ HOẶC Ví dụ
"//div[@class='canvas- graph']//a[@href='/accounting.html'][i[@class='icon-usd']]/following-sibling::h4"0
Cha mẹ trong Selenium là gì?
Parent trong Selenium là một phương thức được sử dụng để truy xuất nút cha của nút hiện tại được chọn trong trang web. Nó rất hữu ích trong trường hợp bạn chọn một phần tử và cần lấy phần tử cha bằng Xpath. Phương pháp này cũng được sử dụng để có được cha mẹ của cha mẹ
Ở đây truy vấn XPath của chúng tôi sẽ giống như
"//div[@class='canvas- graph']//a[@href='/accounting.html'][i[@class='icon-usd']]/following-sibling::h4"1
XPath sử dụng Parent
"//div[@class='canvas- graph']//a[@href='/accounting.html'][i[@class='icon-usd']]/following-sibling::h4"2
Bắt đầu với
Sử dụng chức năng Starts-with, bạn có thể tìm phần tử có thuộc tính tự động thay đổi khi làm mới hoặc các thao tác khác như nhấp, gửi, v.v.
Ở đây truy vấn XPath của chúng tôi sẽ giống như
"//div[@class='canvas- graph']//a[@href='/accounting.html'][i[@class='icon-usd']]/following-sibling::h4"0
"//div[@class='canvas- graph']//a[@href='/accounting.html'][i[@class='icon-usd']]/following-sibling::h4"1
trục Xpath
Bằng cách sử dụng các trục XPath, bạn có thể tìm thấy các phần tử động và rất phức tạp trên một trang web. Các trục XPath chứa một số phương thức để tìm một phần tử. Ở đây, sẽ thảo luận về một số phương pháp