Beruflich Dokumente
Kultur Dokumente
Locators Strategy
Some of Selenium Locator Strategies are :
Id
Name
Link
XPath
CSS
Example 1
Consider Following HTML Snippets
<body>
<form id="loginForm">
<input id="u1" name="username" type="text" />
<input id="n1" name="password" type="password" />
<input id="c1 name="continue" type="submit" value="Login" />
</form>
</body>
Note that <form> , <input> are called tags or elements
Id=u1 , name=username are called attributes.
Example 2
Note that not all HTML are well formed with an id
attribute.
Consider Following HTML Snippets
<body>
<form id="loginForm">
<input id="u1" name="username" type="text" />
<input name="password" type="password" />
<input name="continue" type="submit" value="Login" />
</form>
</body>
Example 3
Consider Following Snippets
<body>
<form id="loginForm">
<input id="u1" name="username" value = "xyz" type="text" />
<input name="username" value="abc" type="password" />
<input name="continue" type="submit" value="Login" />
</form>
</body>
Example 3 - Solution
Refine your locators in following two Filters
Value Filter ( add value filter if you have one )
Index Filter
Solutions:
1.
2.
3.
Example 3 A
This strategy is intended to select links only and selects the
Code Snippets
<div>
<a href="/intl/en/ads/">Advertising Programs</a>
<a href="/services/">Business Solutions</a>
<a href="/intl/en/policies/">Privacy & Terms</a>
</div>
Locators
Example 4
Consider Following Snippets in Example 4
Question 1:
No ID found
No Name found
Lets learn about a new way to locate.
HTML File
XPAth Example 1
Consider Following Snippets
1. <AAA>
2.
<BBB/>
3.
<CCC/>
4.
<BBB/>
5.
<BBB/>
6.
<DDD>
7.
<BBB/>
8.
</DDD>
9.
<CCC/>
10. </AAA>
Question: What Xpath will select complete document ?
Answer: /AAA
XPath Example 2
Consider Following Snippets
1. <AAA>
2.
<BBB/>
3.
<CCC/>
4.
<BBB/>
5.
<BBB/>
6.
<DDD>
7.
<BBB/>
8.
</DDD>
9.
<CCC/>
10. </AAA>
Question: What XPath will select BBB element in Line 7?
Answer: /AAA/DDD/BBB
XPath Example 3
Question : What will be XPath to Select the elements
in red
<AAA>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
</AAA>
Answer: /AAA/BBB[4] Index Starts With 1.
or
/AAA/BBB[last()]
or
//BBB[4]
XPath Example 4
Question: How do you locate password in line 4 using
Example3.html
1. <body>
2. <form id="loginForm">
3.
<input id="u1" name="username" value = "xyz" type="text" />
4.
<input name="username" value="abc" type="password" />
5.
<input name="continue" type="submit" value="Login" />
6.
</form>
7. </body>
XPath Example 5
Question: How Do You Locate Submit Button in
Example5.html
<input type="button" value="Submit!" id="submit_681">
//input[@id=submit_681] is Wrong
Issues: Id is generated dynamically.
//input[starts-with(@id,submit)]
Doubts: How to select the second submit button ?
XPath Example 5
Question: How Do You Locate Submit Button in
Example5.html
<input type="text" style="background-color: rgb(255, 255, 255);" id="91_count" value="" maxlength="3" size="3" name="qty">
//input[@id=91_count] is Wrong
Issues: Id is generated dynamically.
//input[contains(@id,count)]
Doubts: How to select the second Quantity button ?
Path.
CSS Path
Efficient than XPath. Lets Couple of examples to
XPath
CSS Path
/html
html
Locate id=u1 in
example3.html
//input[@id=u1]
#u1 or input#u1 or
input[id='u1']
Locate product 3 in
example5.html
html/body/table/tb DOUBTS
ody/tr[4]/td[1]
//input[startswith(@id,submit)]
input[id^=submit]
//input[contains
(@id,count)]
input[id*=count]