Careeroza
Absolute vs Relative XPath
Two Kinds of XPath
| Kind | Starts with | Example | Use? |
|---|---|---|---|
| Absolute | / from the root | /html/body/div[2]/div/form/input[1] | avoid: breaks when layout changes |
| Relative | // anywhere in the page | //input[@id='email'] | yes, always |
XPath Basics
| XPath | Meaning |
|---|---|
//input | every input element |
//input[@name='email'] | input whose name attribute is email |
//*[@id='cart-count'] | any tag with that id |
//input[@type='email' and @name='email'] | two conditions |
(//div[@class='product-card'])[1] | first product card (XPath counts from 1) |
driver.findElement(By.xpath("//input[@name='email']")).sendKeys("[email protected]");
driver.findElement(By.xpath("(//div[@class='product-card'])[1]//h3")).getText();