This document provides a cheat sheet on different element locators in Selenium such as ID, name, class name, tag name, link text, partial link text, CSS selector, and XPATH. It includes examples of how to use each locator type with the Selenium findElement method and the By class. It also describes how to locate elements based on attributes, child elements, disabled/checked states, and more using both XPATH and CSS selector syntax. Finally, it defines common HTML tags like <ul>, <tr>, <th>, and <img> and their uses.
This document provides a cheat sheet on different element locators in Selenium such as ID, name, class name, tag name, link text, partial link text, CSS selector, and XPATH. It includes examples of how to use each locator type with the Selenium findElement method and the By class. It also describes how to locate elements based on attributes, child elements, disabled/checked states, and more using both XPATH and CSS selector syntax. Finally, it defines common HTML tags like <ul>, <tr>, <th>, and <img> and their uses.
This document provides a cheat sheet on different element locators in Selenium such as ID, name, class name, tag name, link text, partial link text, CSS selector, and XPATH. It includes examples of how to use each locator type with the Selenium findElement method and the By class. It also describes how to locate elements based on attributes, child elements, disabled/checked states, and more using both XPATH and CSS selector syntax. Finally, it defines common HTML tags like <ul>, <tr>, <th>, and <img> and their uses.
This document provides a cheat sheet on different element locators in Selenium such as ID, name, class name, tag name, link text, partial link text, CSS selector, and XPATH. It includes examples of how to use each locator type with the Selenium findElement method and the By class. It also describes how to locate elements based on attributes, child elements, disabled/checked states, and more using both XPATH and CSS selector syntax. Finally, it defines common HTML tags like <ul>, <tr>, <th>, and <img> and their uses.
Element Locators, TagName, Xpath and CSS Cheat Sheet
Different Element Locators available for Browser
Element Locator Command for identifying elements ID driver.findElement(By.id("IdName")) Name driver.findElement(By.name("Name")) ClassName driver.findElement(By.className("Element Class")) Tag Name driver.findElement(By.tagName("HTML Tag Name")) Link Text driver.findElement(By.linkText("LinkText")) Partial Link Text driver.findElement(By.linkText("Partial LinkText")) CSS Selector driver.findElement(By.cssSelector(“css value”))
XPATH driver.findElement(By.xpath("XPath"))
driver – Object/Instance of WebDriver
findElement – WebDriver method By – Pre-defined Class in Selenium id – Element locater/attribute IdName – Locator value
Description Xpath CSS Selector
Whole WebPage /html html Whole WebPage /html/body body body image element //img img Link //a[@href = 'url'] a[href = 'url'] Direct Child //div/a div > a Id //tagName[@id=’idValue’] tagName#idValue Class //tagName[@class=’classValue’] tagName.Value of Class attribute Attribute //tagname[@attribute-name=’value1′] tagName[attribute=Value of attribute] Multiple //input[@type='submit' and @name='btnLogin'] tagname[attribute1='value1'][attribute2=' Attributes value2'] Contains //*[contains(@type,'sub')] <HTML tag><[attribute*=sub string]> Starts with //tagname[starts-with(@attribute, ‘Start value’)] <HTML tag><[attribute^=prefix of the string]> Ends with //tagname[ends-with(@attribute, ‘End value’)] <HTML tag><[attribute$=suffix of the string]> Matches //*[matches(@type,'value')] N/A First Child //ul[@id=’list’]/li[1] ul#list li:first-child Last Child //ul[@id=’list’]/li[last()] ul#list li:last-child nth Child //ul[@id=’list’]/li[3] ul#list li:nth-child(3) Text Value //td[text()=‘textname'] N/A Element preceding //E2/preceding-sibling::E1 N/A some sibling
Sibling element //E/preceding-sibling::*[1] N/A
immediately preceding User interface //E[@disabled] E:disabled element that is disabled Checkbox (or //*[@checked] *:checked radio button) that is checked Text Value //td[text()=‘textname'] N/A
Important Tagname and their Description
Tag Name Specification <ul> Defines an unordered list of items. <tr> Defines a row of cells in a table. <title> Represents title to an HTML document. <th> Used for creates header of a group of cell in HTML table. <table> Used to defines a table in an HTML document. <tbody> Used for grouping table rows. <td> Used for creates standard data cell in HTML table. <span> Used to grouping and applying styles to inline elements. <section> Used to divide a document into number of different generic section. <select> Used to create a drop-down list. <menu> Used to display a unordered list of items/menu of commands. <li> Define a list item either ordered list or unordered list. <iframe> Defines a inline frame that embedded external content into current web document. <img> Used to insert image into a web document. <input> Define a get information in selected input <div> Define a division part <body> Defines a main section(body) part in HTML document <br /> Specific a single line break <button> Specifies a press/push button <a> Specific a anchor (Hyperlink)