0% found this document useful (0 votes)
1K views

Exhaustive WebDriver Locators Cheat Sheet Csharp

This document provides a comparison of XPath and CSS locators for selecting elements in HTML documents. It lists over 50 examples of locators using both XPath and CSS syntax along with brief explanations. The locators target a variety of elements like images, tables, links, forms and use attributes, indexing, relationships between elements and other selection methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Exhaustive WebDriver Locators Cheat Sheet Csharp

This document provides a comparison of XPath and CSS locators for selecting elements in HTML documents. It lists over 50 examples of locators using both XPath and CSS syntax along with brief explanations. The locators target a variety of elements like images, tables, links, forms and use attributes, indexing, relationships between elements and other selection methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Most Exhaustive

http://automatetheplanet.com
XPath Locators CSS Locators
XPath Locator Explanation CSS Locator Explanation
//img image element ul#myId ul element with @id= myId

//img[@id='myId'] image element with @id= 'myId' #myUniqueId any element with @id= myId
image elements with @id not equal to ul.myForm ul element with @class = myForm
//img[@id!='myId']
'myId' any element with @classes = myform and
.myForm.front
//img[@name] image elements that have name attribute front
ul#myUniqueId > li direct child element
//*[contains(@id, 'Id')] element with @id containing
ul#myUniqueId li sub child element
//*[starts-with(@id, 'Id')] element with @id starting with
ul[name = ul element with attributes @name
//*[ends-with(@id, 'Id')] element with @id ending with "automateName"][style = =automateName and @style= style
"style_name"] name
//*[matches(@id, 'r')] element with @id matching regex r
ul[id = "myId"] 'ul' element with @id='myId'
//*[@name='myName'] image element with @name= 'myName'
ul[@id] elements with @id attribute
//*[@id='X' or @name='X'] element with @id X or a name X elements with name N and specified value
*[name='N'][value='v]
element with @name N & specified v
//*[@name="N"][@value="v"]
@value v all elements with an attribute beginning
ul[id ^= "my"]
//*[@name="N" and element with @name N & specified with my
@value="v"] @value v all elements with an attribute ending with
ul[id$= "Id"]
//*[@name="N" and element with @name N & not specified Id
not(@value="v")] @value v all elements with an attribute containing the
ul[id *= unique"]
//input[@type="submit"] input of type submit substring unique
all elements with an attribute containing the
returns <section> if it has an <h1> ul[id ~= unique"]
//section[//h1[@id='hi']] word unique
descendant with @id= 'hi'
ul#myUniqueId li:first-child first child element
//table[count(tr) > 1] return table with more than 1 row
ul#myUniqueId li:nth-of-
//*[.="t"] element containing text 't' exactly first child element
type(1)
//a[contains(text(), "Log ul#myUniqueId li:last-child last child element
anchor with inner text containing 'Log Out'
Out")]
ul#myUniqueId li:nth-of-
//a[not(contains(text(), "Log anchor with inner text not containing 'Log last child element
type(3)
Out"))] Out'
all <p> elements that are a direct
//a[@href="url"] anchor with target link 'url' div > p
descendant of a <div> element
//img/*[1] first child of element img all <p> elements that are the next sibling of
div + p
a <div> element (i.e. placed directly after)
//ul/child::li first child 'li' of 'ul'
all <p> elements that follow, and are
//img[1] first img child div ~p
siblings of <div> elements
//img/*[last()] last child of element img a:link all unvisited links

//img[last()] last img child a:visited all visited links


a:hover all links on mouse hover
//img[last()-1] second last img child
input:active every active <input> element
//input/following-sibling::a 'a' following some sibling 'input'
input:disabled every disabled <input> element
//a/following-sibling::* sibling element immediately following 'a'
input:enabled every enabled <input> element
//input/preceding-sibling::a 'a' preceding some sibling 'input'
sibling element immediately preceding input:focus the <input> element which has focus
//input/preceding-sibling::*[1]
'input' <input> elements with the readonly
input:read-only
//img[@id='MyId']::parent/* the parent of image with id attribute specified
<input> elements with the required
//*[@id="TestTable"]//tr[3]//td[ input:required
cell by row and column attribute specified
2]
cell immediately following cell containing 't' input:checked checkbox (or radio button) that is checked
//td[preceding-sibling::td="t"]
exactly
//td[preceding- form myForm.front + ul next sibling
cell immediately following cell containing 't'
sibling::td[contains(.,"t")]]
a:contains('Log Out') anchor with inner text containing 'Log Out'
//input[@checked] checkbox (or radio button) that is checked
a[href='url'] anchor with target link 'url'
//a[@disabled] all 'a' elements that are disabled
#TestTable tr:nth-child(3) cell by row and column (e.g. 3rd row, 2nd
//a[not(@disabled)] all 'a' elements that are not disabled td:nth-child(2) column)

//a[@price > 2.50] 'a' with price > 2.5 td:contains('t') ~td cell immediately following cell containing 't'
all <p> elements with a @lang attribute
//ul[*] 'ul' that has children p:lang(language)
equal to language

FindsBy Attributes Default FindElement


[FindsBy(How = How.Id, Using = "userName")] _driver.FindElement(By.Id("userName"));
[FindsBy(How = How.ClassName, Using = "panel other")] _driver.FindElement(By.ClassName("panel other"));
[FindsBy(How = How.CssSelector, Using = "#userName")] _driver.FindElement(By.CssSelector("#userName"));
[FindsBy(How = How.LinkText, Using = "Automate Planet")] _driver.FindElement(By.LinkText("Automate The Planet"));
[FindsBy(How = How.Name, Using = "webDriverCheatSheet")] _driver.FindElement(By.Name("webDriverCheatSheet"));
[FindsBy(How = How.PartialLinkText, Using = "Automate")] _driver.FindElement(By.PartialLinkText("Automate"));
[FindsBy(How = How.TagName, Using = "a")] _driver.FindElement(By.TagName("a"));
[FindsBy(How = How.XPath, Using = "//*[@id='pan']/div ")] _driver.FindElement(By.XPath("//*[@id='panel']/div/h1"));
private IWebElement _myElement;

You might also like