How to dynamically generate xpath by xpath axis 

xpath axis How to dynamically generate xpath by xpath axis

xpath axis image


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 :

  1. //span[.='test'] select a span element whose innertext is ‘test’
  2. //span[.='test']/parent::div select its parent div note of the specified span node
  3. //a[.='Add'] select a button element whose innertext is’Add’
  4. //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']


Author: BiLi on September 14, 2010
Category: XPath
Tags:
Google Webdriver Forum -The Online Community for Google Webdriver and Selenium RC Users and Professionals
2 responses to “How to dynamically generate xpath by xpath axis”
  1. admin says:

    Here is another example:
    I want to get the id of the div element from the following code:

    Hello

    testing

    here is the code: //span[contains(text(),'testing')]/../@id

  2. Pavandeep Puddupakkam says:

    Here is an example of checking for an element that is not selected
    Number c = selenium.GetXpathCount(“//select[@id='ctl00_MainContent']/option[(not(@selected))]“);

Leave a Reply

Last articles