0

This question already has an answer here:

I have web element set like below ..

<div class="dxnb-headerCollapsed" id="ctl00_LeftPanel_LeftNavigationMenu_nbMenu_GHC1">
            <i class="fa fa-handshake-o fa-2x  fa-fw" style=""></i> My Team
        </div>

There, the number in GHC1 change depending on place, but text "My Team" is unique for particular set of element.

I tried to locate it with

By.XPATH,'//*[contains(@id,"ctl00_LeftPanel_LeftNavigationMenu_nbMenu_GHC") AND contains(text()="My Team")]'

But failed. How to do that correctly ? can not use by.text since "My Team" text could be exist in other parts of web page.

marked as duplicate by Bharat Mane, Kate Paulk Mar 9 '18 at 12:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

Why are you not going with 'class'. Now as per your details the text in ID attribute gets changed everytime, so in this case 'starts-with' function works well.

See below some examples, it might works in your case-

//div[contains(@class,'dxnb-headerCollapsed') and starts-with(@id,'ctl00_LeftPanel_LeftNavigationMenu')]//i[1];

or Just Try-

//div[contains(@class,'dxnb-headerCollapsed') and starts-with(@id,'ctl00_LeftPanel_LeftNavigationMenu')];

OR

//*[contains(.,"My Team")];
  • does this works for you? – Bharat Mane Mar 20 '18 at 12:54
0

IF text "My Team" is unique for particular set of element then as per my understanding we are not having 'My Team' anywhere on the page, We can directly use syntax:

//div[text()='My Team']

//div[contains(text(),'My Team')]

Most of the software testing services company use the same approach. If you want to use the syntax provide by you then you can directly concatenate the text with the id without using AND also like:

//div[contains(@id,'ctl00_LeftPanel_LeftNavigationMenu_nbMenu')][text()='My Team']
//div[contains(@id,'ctl00_LeftPanel_LeftNavigationMenu_nbMenu')] AND [text()='My Team']

//div[contains(@id,'ctl00_LeftPanel_LeftNavigationMenu_nbMenu')] [contains(text()='My Team')]
//div[contains(@id,'ctl00_LeftPanel_LeftNavigationMenu_nbMenu')] AND [contains(text()='My Team')]

Please let me know if the syntax provided above is working.

  • any of above syntax did not work. – 1234 Mar 12 '18 at 23:35

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