可以用FindBy注释来代替driver.findelement(By x).
//定义一个element
@FindAll({@FindBy(xpath = "yourxpath")})
public WebElement; resultElements;
//一次定义多个elements
@FindAll({@FindBy(xpath = "yourfirstxpath"),@FindBy(xpath = "yoursecondxpath"),@FindBy(xpath = "yourThirddxpath")})
public List<WebElement> resultElements;
//一个项目中的例子
package com.perficient.test.cat.com.reg.basepages;
import java.util.List;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class Augers{
//Labels for New Products and Rental Products
@FindBy(xpath = "//*[@class='filters']/li/input")
public List<WebElement> listOfLabelsOfBrowserInventory;
//Augers title
@FindBy(xpath="//div/h2")
public WebElement titleOfAugers;
//currency dropdown
@FindBy(xpath="//div/select[@name='currency']")
public WebElement dropdownOfCurrency;
//MSRP disclaimer
@FindBy(xpath="//div/select[@name='currency']")
public WebElement MSRPDisclaimer;
//Availability Statement
@FindBy(xpath="//div[@class='availability-statement']")
public WebElement Availabity;
}
FindBy的10种定位元素的方法。