0% found this document useful (0 votes)
7 views14 pages

Xpath

The document provides a guide on using XPath locators to identify web elements in automation testing. It includes examples of locating elements by ID, class name, name, tag name, text, and using functions like contains and starts-with. The examples demonstrate how to retrieve various elements such as buttons, input fields, and headings from a webpage.

Uploaded by

Ch Zaid kumboh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views14 pages

Xpath

The document provides a guide on using XPath locators to identify web elements in automation testing. It includes examples of locating elements by ID, class name, name, tag name, text, and using functions like contains and starts-with. The examples demonstrate how to retrieve various elements such as buttons, input fields, and headings from a webpage.

Uploaded by

Ch Zaid kumboh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Formula : xpath = //tagname[@attribute='value']

Tagname = Type of Web Element (e.g. input, button, href, etc.)


Attribute = Type of Locator. (e.g. id, name, className)
Value = text of the type of the locator

driver.get("https://www.automationexercise.com/products");

// XPath Locator By ID
WebElement productSearch =
driver.findElement(By.xpath("//*[@id='search_product']"));

// XPath Locator By ClassName


WebElement productContainer =
driver.findElement(By.xpath("//*[@class='features_items']"));
// XPath Locator By Name
WebElement searchBox =
driver.findElement(By.xpath("//input[@name='subject']"));

// XPath Locator By TagName


WebElement addToCartButton = driver.findElement(By.xpath("//button"));
WebElement Titles = driver.findElement(By.xpath("//h4"));

WebElement Images = driver.findElement(By.xpath("//img"));


WebElement Headings = driver.findElement(By.xpath("//h2[@class='title text-
center']"));

// XPath Locator by Text


WebElement addToCartText = driver.findElement(By.xpath("//*[text()='Add to
cart']"));

WebElement LocateLink = driver.findElement(By.xpath("//h2[text()='Brands']"));


WebElement LocateHeading = driver.findElement(By.xpath("//a[text()='View
Product']"));

WebElement LocateButton = driver.findElement(By.xpath("//a[text()='Add to


cart']"));
// XPath Locator by Contains
WebElement viewProduct = driver.findElement(By.xpath("//*[contains(text(),
'View Product')]"));

// XPath Locator by Starts With


WebElement StrtssearchField = driver.findElement(By.xpath("//*[starts-
with(@id, 'search')]"));
// XPath Locator By Name
WebElement nameee = driver.findElement(By.xpath("//*[@name='name']"));

// XPath Locator by Contains


WebElement ContainsName =
driver.findElement(By.xpath("//input[contains(@placeholder, 'Name')]"));
WebElement ContainsEmail =
driver.findElement(By.xpath("//input[contains(@name, 'email')]"));

WebElement Containscontact =
driver.findElement(By.xpath("//h2[contains(text(), 'Contact')]"));
WebElement ContainsSubmit =
driver.findElement(By.xpath("//input[contains(@value, 'Submit')]"));
WebElement ContainsMessage =
driver.findElement(By.xpath("//textarea[contains(@id, 'message')]"));
WebElement Containsupload =
driver.findElement(By.xpath("//input[contains(@name, 'upload')]"));

WebElement ContainsContactUS =
driver.findElement(By.xpath("//a[contains(@href, 'contact_us')]"));
// XPath Locator by Starts With
WebElement StrtsGet = driver.findElement(By.xpath("//h2[starts-
with(text(),'Get')]"));
WebElement StrtsName = driver.findElement(By.xpath("//input[starts-
with(@name,'name')]"));
WebElement StrtsSubm = driver.findElement(By.xpath("//button[starts-
with(@type,'subm')]"));

You might also like