XPath Contains:Selenium 中的文本、后继同级和祖先
XPath Contains 是什么?
XPath contains 是 XPath 表达式中的一个函数,用于搜索包含特定文本的网页元素。我们可以使用 XPath contains() 函数在整个网页中提取所有与给定文本值匹配的元素。XPath 中的 contains 具有通过部分文本查找元素的能力。
示例 – contains 文本
这里我们搜索一个锚点 .contains 文本为“SAP M”。
"//h4/a[contains(text(),'SAP M')]"
注意:您可以在此链接上练习以下 XPath 练习:https://demo.guru99.com/test/selenium-xpath.html
如果简单的 XPath 无法为我们的测试脚本找到复杂的网页元素,我们需要使用 XPath 1.0 库中的函数。结合这些函数,我们可以创建更具体的 XPath。
XPath 中的后继同级
Selenium Webdriver 中的同级 (Sibling) 是一个函数,用于获取与父元素同级的网页元素。如果已知父元素,则可以轻松找到或定位使用 selenium webdriver 中 Xpath 表达式的同级属性的网页元素。
XPath 中的同级示例
这里,我们根据“a”的同级元素查找“h4”
"//div[@class='canvas- graph']//a[@href='/accounting.html'][i[@class='icon-usd']]/following-sibling::h4"
祖先 (Ancestor):为了根据父元素查找元素,我们可以使用 XPath 的祖先属性。
让我们通过一个例子来理解这 3 个函数 –
测试步骤
注意:由于本教程创建日期,Guru99 的主页已更新,请使用演示站点运行测试
- 前往 https://demo.guru99.com/test/guru99home/
- 在“我们最受欢迎的一些课程”部分,搜索所有文本为“SELENIUM”的 WebElement 的同级网页元素
- 我们将使用 XPath 文本包含、祖先和同级函数查找元素。
使用 Contains Text 和 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(); } }
输出将如下所示:
Selenium 中的 XPath Ancestor
Selenium 中的 XPath Ancestor 是一个函数,用于在指定层查找特定成员的祖先。可以明确指定要返回的祖先级别或祖先相对于成员级别的级别。它返回祖先的层级步数,定位用户指定的祖先。
现在假设我们需要在“热门课程”部分中借助文本为“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,您可以在 XPath 表达式中设置 2 个条件。
- 在 AND 的情况下,2 个条件都必须为真才能找到元素。
- 在 OR 的情况下,2 个条件中的任何一个为真即可找到元素。
这里我们的 XPath 查询将如下所示
Xpath=//*[@type='submit' OR @name='btnReset']
Xpath=//input[@type='submit' and @name='btnLogin']
测试步骤
- 前往 https://demo.guru99.com/v1/
- 在该部分中,将使用上述演示站点通过 XPath 的不同函数搜索元素。
您将使用 AND 和 OR、父级、starts-with 和 XPath 轴查找元素
AND OR 示例
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://guru99.com.cn/"); //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(); } }
Selenium 中的 XPath Parent
Selenium 中的 Parent 是一种用于检索网页中当前选中节点的父节点的方法。当您选择一个元素并需要使用 Xpath 获取父元素时,这非常有用。此方法也用于获取父级的父级。
这里我们的 XPath 查询将如下所示
Xpath=//*[@id='rt-feature']//parent::div
使用 Parent 的 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://guru99.com.cn/"); //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
使用 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://guru99.com.cn/"); //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 轴包含几种查找元素的方法。这里,我们将讨论几种方法。
following:此函数将返回特定组件的紧邻元素。
这里我们的 XPath 查询将如下所示
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://guru99.com.cn/"); //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(); } }
Preceding:此函数将返回特定元素的先行元素。
这里我们的 XPath 查询将如下所示
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://guru99.com.cn/"); //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) Descendant:此函数将返回特定元素的后代元素。
这里我们的 XPath 查询将如下所示
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://guru99.com.cn/"); //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 contains、parent、ancestors、following-sibling 等。
- 借助这些函数,您可以创建复杂的 XPath 表达式。如果您需要专门了解如何在 XPath 中使用 contains,则 selenium 中的 contains 函数提供了一种基于部分文本匹配搜索网页元素的简单方法。