Monday, October 17, 2016

XPath Selectors.

driver.findElement(By.xpath(“ // ...”));
element.findElement(By.xpath(“ // ...”));
element.findElement(By.xpath(“. // ...”));

By Index
  • //td[text()='Books']
  • (//li)[1]
  • (//li)[last()]
  • (//li)[1]/..
  • (//li)[last()]/..
  • //nodename[3] - 3rd child of its parent node
  • (//nodename)[3] - 3rd nodename element

Xpath Axes
  • AxisName::nodetest[predicate]
  • ex. //td[text()='Books']/ancestor::table
  • AxisName
    • ancestor
    • ancestor-or-self
    • descendant
    • descendant-or-self
    • following
    • following-sibling
    • preceding
    • preceding-sibling
    • parent
    • child
    • self
driver.findElement(By.xpath("//td[text()='Books']/ancestor::table"));

// driver.findElement(By.xpath(“//...”)); 
// element.findElement(By.xpath(“//...”)); 
// element.findElement(By.xpath(“.//...”));

// Notice the difference
//
// <div id="id0">
//        <p>Hello</p>
// </div>
// <div id="id1">
//        <p>World</p>
// </div>

// current node is "driver"
driver.findElement(By.id("id1"))
        .findElement(By.xpath("//p")).getText()  //=> Hello  
driver.findElement(By.id(“id1”))
        .findElement(By.xpath(".//p")).getText() //=> World


No comments:

Post a Comment