I have situation when i have click main menu options it will be display 10 different sub-menu options. In all sub-menu section is available 2nd sub-menu options which is available more than 20 menu options and all are dynamic.

<a id="ui-id-11" class="ui-corner-all" href="https://www.xyz.com/abc" tabindex="-1" role="menuitem">

This above sub-menu - my xpath is - //*[@id='ui-id-11']/span

<a id="ui-id-12" class="ui-corner-all" href="https://www.abc.com/abc" tabindex="-1" role="menuitem">
<span>Riding Gear</span>

This above sub-menu's if- my xpath is - //*[@id='ui-id-12']/span

How to handle all the sub-menu options to the single Xpath?

  • you cannot have a single xpath for multiple sub-menu.what are you exactly trying to achieve. – Alok Jan 18 '18 at 12:03
  • 1
    What are you trying to do? Are you trying to locate a specific sub-menu item which may not have the same ID each time you access it? If so, this is a duplicate as @BharatMane suggests – Kate Paulk Jan 18 '18 at 12:23
  • yup..as per his details given in the question may he wants to locate a specific sub-menu item which may not have the same ID's every time. So, I think above question helps a lot. – Bharat Mane Jan 18 '18 at 12:28

You can use X-Path predicate starts-with as follows -


Update : But certainly this would not help your cause - You may need to use another predicate like contains to check whether particular text is present or not.


But in that case, whatever I suggested earlier in the answer is not useful. Better way is to create XPath dynamically -

String dropDwonOption = //assign value here dynamically like Helmet, Riding Gear
String xpath = "/span[contains(text(),'"+dropDwonOption+"')]";

And then use this xpath.

  • All right but, using this XPath it will finds all the sub-menus who have the same id's starting from 'ui-id-11', 'ui-id-12', 'ui-id-13' in this case what? – Bharat Mane Jan 18 '18 at 12:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.