在这里仅记录一下常用的定位方式:
- 1. id定位
@FindBy(id = "EmailInvoice")
private WebElement emailInvoiceCheckBox;
- 2. name/class等标签定位
@FindBys(@FindBy(className = "Content"))
private List<WebElement> resolutionText;
- 3. css定位
//一级一级查找# // . //:nth-child
@FindBy(css = "#InvoiceInfoBlock .invoiceInfoRow:nth-child(2)")
private WebElement invoiceNumber;
//使用部分属性值匹配
@FindBy(css = "img[title*='4.00']")
private WebElement surveryRatingImg;
- 4. xpath定位
//通过绝对路径做定位
By.xpath("html/body/div/form/input")
//通过相对路径定位
By.xpath("//input")
//通过元素索引定位
By.xpath("//input[4]")
//使用xpath属性定位
By.xpath("//input[@id='kw1']")
By.xpath("//input[@type='name' and @name='kw1']")
//使用部分属性值匹配
By.xpath("//input[start-with(@id,'valueA'")
By.xpath("//input[ends-with(@id,'valueB'")
By.xpath("//input[contains(@id,'valueC')]")
/**
包含noteDescription的span的父亲节点的第一个兄弟节点下的第一个div下的第五个a.
其中
1. /..
/ 从根节点选取 .. 选取当前节点的父节点
2. following-sibling
选取当前节点后的同级节点
preceding-sibling
选取当前节点前的同级节点
*/
By.xpath("//span[contains(text(),'" + noteDescription + "') and @id = 'noteText']/../following-sibling::div[1]/a[5]")