如果简单的XPath不能为我们的测试脚本找到复杂的Web元素,我们需要使用XPath1.0库中的函数。让我们讨论一下3个这样的函数-
- Contains
- Sibling
- Ancestor
- And OR
- Parent
- Starts with
- XPath Axes
Selenium中的 contains() 是什么?
Selenium中的 contains() 是XPath表达式中的一个函数,用于搜索包含特定文本的Web元素。XPath中的CONTAINS能够查找包含部分文本的元素。
例如。在这里,我们正在搜索一个锚。包含‘SAP M’形式的文本。
"//h4/a[contains(text(),'SAP M')]"
注意:可以在此http://www.itxiaonv.com/test/selenium-xpath.html上练习以下xpath练习
Selenium Webdriver中的兄弟是什么?
Selenium Webdriver中的同级是用于获取web元素的函数,该web元素是父元素的同级元素。如果父元素是已知的,那么可以很容易地找到或定位可以使用Selenium Webdriver中XPath表达式的兄弟属性的web元素。
XPath中的兄弟元素示例:在这里,根据‘a’的兄弟元素,我们找到了‘h4’。
"//div[@class='canvas- graph']//a[@href='/accounting.html'][i[@class='icon-usd']]/following-sibling::h4"
祖先:要在父元素的基础上查找元素,可以使用XPath的祖先属性。
让我们通过一个示例来理解这3个函数-
测试步骤
注意:自教程创建之日起,Guru99的主页已更新,因此使用演示站点运行测试
- 访问http://demo.guru.com/test/guru99home/
- 在“几个最受欢迎的课程”一节中,搜索与文本为“Selenium”的WebElement同级的所有Web元素。
- 我们将使用XPath文本包含、祖先和兄弟函数查找元素。
使用CONTAINS和XPath同级
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("http://www.itxiaonv.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 WebDriver中的Ancestor
Selenium Webdriver中的祖先是一个函数,用于在指定层查找特定成员的祖先。它返回上级的分层步骤数,定位用户需要的指定上级。
现在假设我们需要借助文本为“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("http://www.itxiaonv.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表达式中加入两个条件。
- 在AND的情况下,两个条件都应该为真,则只有它找到该元素。
- 在OR的情况下,这两个条件中的任何一个都应该为真,那么只有它才能找到该元素。
在这里,我们的XPath查询将如下所示
Xpath=//*[@type='submit' OR @name='btnReset']
Xpath=//input[@type='submit' and @name='btnLogin']
测试步骤:
- 访问http://demo.guru.com/v1/
- 在本节中,将使用上面的演示站点搜索具有不同功能的XPath元素。
将使用AND AND OR、PARENT、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://www.itxiaonv.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();
}
}
Selenium的母体是什么?
Selenium中的Parent是用于检索在网页中选择的当前节点的父节点的方法。此方法还用于获取父级的父级。
在这里,我们的XPath查询将如下所示
Xpath=//*[@id='rt-feature']//parent::div

使用父级的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.itxiaonv.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
使用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.itxiaonv.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=//*[@type='text']//following::input

使用以下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.itxiaonv.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
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.itxiaonv.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)Destendant:此函数将返回特定元素的后代元素。
在这里,我们的XPath查询将如下所示
Xpath= //*[@id='rt-feature']//descendant::a

使用子体的XPath
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.itxiaonv.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 CONTAINS、PARENT、STORARS、FLOGING-SIBRING等。
- 在这些函数的帮助下,可以创建复杂的XPath表达式。