Thursday, November 3, 2022

Selenium - Xpath locator

  • By Tagname   //tagname[@attribute='value']
driver.findElement(By.xpath("//input[@placeholder='Name']"))

 

  • By index of the attribute //tagname[@attribute='value'][index]
 driver.findElement(By.xpath("//input[@type='text'][2]"))

        (Used when your attribute and the value are same for multiple elements) 


  •  By tagnames when there are many instances of the childtag. //parenttag/childtag[index])

driver.findElement(By.xpath("//form/input[3]"))

(Use index when there are many instances of child tag and you have to locate one of them.)


  • By parent to child-tag  //parenttag[@attribute='value']/childtag[index])

driver.findElement(By.xpath(" //div[@class='forgot-pwd-btn-
conainer']/button[1]")) 

 

  •  With regular expression  //tagname[contains(@class,'value')]

driver.findElement(By.xpath("//button[contains(@class,'submit')]"))