Selenium中定位方式记录

来源:互联网 发布:青牛软件 上市 编辑:程序博客网 时间:2024/05/16 12:28

在这里仅记录一下常用的定位方式:

  • 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]")
0 0
原创粉丝点击