Xpath in Selenium
Different ways to generate XPath
in Selenium
1àGenerating Xpath on the
behalf of Html Element attributes
//tagname[@propname=’value’]
So in Real
Time
//input[@id=’f_id’]
//input[@name=’f_name’]
//input[@class=’f_class’]
OR
//*[@id=’f_id’] Here * means any tagname
//input[@*=’f_id’] Here * means any attribute
1.1 Using More than 1 attribute with Conditional OR , Conditional And
Conditional OR: We can set 2
or more attribute, if any of them is satisfied action will took place
Using Or Condition, while creating Xpath, will work in any
match either first or 2nd
//a[@class='folder-name'] | //a[@data-action='open-folderview']
Path1 OR Path2
Conditional AND :
2à Locating element by
the element text (if element text seems to be not changing)
1à By the Text(inner html) of the element
//a[text()='Mail']
2à When we don’t have complete text information, we
just know the starting text
//a[starts-with(text(),'Mail')]
3à When we don’t have complete text information, we
just know the part of text
//a[contains(text(),'Mail')]
Using last keyword
& Position seems not working in
Selenium
Last()
Below code will check or uncheck the second
last checkbox and third last checkbox respectively.
selenium.click("xpath=(//input[@type='submit'])[last()-1]");
selenium.click("xpath=(//input[@type='submit'])[last()-2]");
xpath=(//input[@type='checkbox'])[last()-2]
Node Set : position()
If you want to select any object based on their
position using xpath then you can use position() function in xpath.
You want to select second checkbox and forth
checkbox then use below command
selenium.click("xpath=(//input[@type='checkbox'])[position()=2]");
selenium.click("xpath=(//input[@type='checkbox'])[position()=4]")
selenium.click("xpath=(//input[@type='checkbox'])[position()=2]");
selenium.click("xpath=(//input[@type='checkbox'])[position()=4]")
Position()=2
above code will select second and forth
checkbox respectively.
xpath=(//input[@type='checkbox'])[position()=2] // Seems position starts from 1
4à Locating
element by position
//li[@class='launcher'][3]/a
It will search li with class launcher, search and move to 4th
existence and then go to its child
//li[@class='launcher placeholder'][2]/a
xpath=(//input[@class='reflect-selection'])[3]
5à Locating element with Dual Search
First Search for element by using any existing technique
Then make another search in its child by using another
technique
//div[@class='window-head']/descendant::a[@title='Guidance']
Subscribe to:
Post Comments
(
Atom
)
very useful info about selenium, you can contribute to selenium-cucumber
ReplyDeletemore info : http://seleniumcucumber.wordpress.com/