Upload
dfw2030
View
227
Download
0
Embed Size (px)
Citation preview
8/10/2019 WebElement Identification Techniques
1/19
WEB ELEMENT IDENTIFICATION TECHNIQUES
8/10/2019 WebElement Identification Techniques
2/19
Locators Strategy
Some of Selenium Locator Strategies are :
Id
Name
Link XPath
CSS
8/10/2019 WebElement Identification Techniques
3/19
Example 1
Consider Following HTML Snippets
8/10/2019 WebElement Identification Techniques
4/19
Example 2
Note that not all HTML are well formed with an idattribute.
Consider Following HTML Snippets
Q1. How do you locate password field ?
Ans: By using name : name=password
8/10/2019 WebElement Identification Techniques
5/19
Example 3
Consider Following Snippets
Q. How do you locate password field ?
Ans: name=username => It will point to the usernameas username comes before password.
8/10/2019 WebElement Identification Techniques
6/19
Example 3 - Solution
Refine your locators in following two Filters
Value Filter ( add value filter if you have one )
Index Filter
Solutions:1. name= username value=abc locate password field
2. name = username type=passwordWill not locateanything
3. name = username index=1 Will locate passoword
8/10/2019 WebElement Identification Techniques
7/19
Example 3 A
This strategy is intended to select links only and selects theanchor element containing the specified text:
Code Snippets
Advertising Programs
Business Solutions
Privacy & Terms
Locatorslink= Advertising Programslink = Business Solutions
8/10/2019 WebElement Identification Techniques
8/19
Example 4
Consider Following Snippets in Example 4
Question 1:
How to locate product 2 ?
Solution No ID found
No Name found
Lets learn about a new way to locate.
8/10/2019 WebElement Identification Techniques
9/19
How Do You Locate a File in a Computer
8/10/2019 WebElement Identification Techniques
10/19
HTML File
8/10/2019 WebElement Identification Techniques
11/19
Lets Trace To Element
To product 1 : /html/body/table/tbody/tr[2]/td[1]
Similarly trace some other elements.
This is Called Xpath. XPath describes paths to elements of XML file same
was as OS leads to path in file system.
Almost like a mini programming language withcapabilities like functions, expressions.
8/10/2019 WebElement Identification Techniques
12/19
XPAth Example 1
Consider Following Snippets1.
2.
3.
4. 5.
6.
7.
8.
9. 10.
Question: What Xpath will select complete document ?Answer: /AAA
8/10/2019 WebElement Identification Techniques
13/19
XPath Example 2
Consider Following Snippets1.
2.
3.
4. 5.
6.
7.
8.
9. 10.
Question: What XPath will select BBB element in Line 7?Answer: /AAA/DDD/BBB
8/10/2019 WebElement Identification Techniques
14/19
XPath Example 3
Question : What will be XPath to Select the elementsin red
Answer: /AAA/BBB[4] Index Starts With 1.or
/AAA/BBB[last()]or
//BBB[4]
8/10/2019 WebElement Identification Techniques
15/19
XPath Example 4
Question: How do you locate password in line 4 using
Example3.html
1.
2. 3.
4.
5.
6.
7.
//element name[@attribute=attribute value]
Element name = input
Attribute names = { name=username , value = abc , type=password}
//input[@value=abc] or //input[@name=username][2]
8/10/2019 WebElement Identification Techniques
16/19
XPath Example 5
Question: How Do You Locate Submit Button inExample5.html
//input[@id=submit_681] is Wrong
Issues: Id is generated dynamically.
//input[starts-with(@id,submit)]
Doubts: How to select the second submit button ?
8/10/2019 WebElement Identification Techniques
17/19
XPath Example 5
Question: How Do You Locate Submit Button inExample5.html
//input[@id=91_count] is Wrong
Issues: Id is generated dynamically.
//input[contains(@id,count)]
Doubts: How to select the second Quantity button ?
8/10/2019 WebElement Identification Techniques
18/19
FireBug and Fire Path
Explain How To Install and Work With Fire Path
and Fire Bug.
Show how to capture Xpath and CSS Path using FirePath.
8/10/2019 WebElement Identification Techniques
19/19
CSS Path
Efficient than XPath. Lets Couple of examples toUnderstand CSS Path Syntax.Question XPath CSS Path
Locate complete html in
example3.html
/html html
Locate id=u1 inexample3.html
//input[@id=u1] #u1 or input#u1 orinput[id='u1']
Locate product 3 inexample5.html
html/body/table/tbody/tr[4]/td[1]
DOUBTS
Locate submit button inexample5.html
//input[starts-with(@id,submit)]
input[id^=submit]
Locate qty filed inexample5.html
//input[contains(@id,count)]
input[id*=count]