We can dynamically generate xpath for a control on a web page by xpath axis.
For example, we want to locate “Add ” button by its corresponding category name, say “test” or “driving instructor”.
Then We can dynamically generate xpath for each add button using its corresponding category name by axis :
- //span[.='test'] select a span element whose innertext is ‘test’
- //span[.='test']/parent::div select its parent div note of the specified span node
- //a[.='Add'] select a button element whose innertext is’Add’
- //span[.='test']/parent::div/descendant::a[.='Add'] select a button element whose innertext is’Add’ and who is a descendant of the div note //span[.='test']/parent::div
Then we can locate each button by its xpath “//span[.='test']/parent::div/descendant::a[.='Add'] ” or “//span[.='driving instructor']/parent::div/descendant::a[.='Add']


Here is another example:
I want to get the id of the div element from the following code:
testing
here is the code: //span[contains(text(),'testing')]/../@id
Here is an example of checking for an element that is not selected
Number c = selenium.GetXpathCount(“//select[@id='ctl00_MainContent']/option[(not(@selected))]“);