5

I have code that includes the following label

<label _ngcontent-c18="" translate="" for="517390_radio-button">Missing Applicant's Signature</label>

However the label and the code for it appears twice on the page.
Also the ID is unique and generated new every time.
I would like to create an xpath selector to select the first occurrence for the element

Could somebody please help to find that

1
  • Its not that here.The issue is not dynamically changing ids.Need to select the first occurrence of field which is having multiple occurrence on a page
    – RRR
    May 2, 2018 at 12:23

4 Answers 4

7

In XPath there are a couple of ways you could try this.

Match by text (can be partial text too)

(//label[contains(.,'Missing Applicant')])[1]

Match by tag (if that helps isolate the label from other labels)

(//label[@_ngcontent-c18])[1]

Match by partial tag value

(//label[contains(@for,'radio-button')])[1]

Note the brackets surrounding the selectors before the index If you leave these out then you will match the first element of each branch of the DOM which matches your selector, e.g., the following will return the set of the first label element in each branch with a for tag containing the value 'radio-button'. NOT the first matching element from that set!

//label[contains(@for,'radio-button'][1]

Looking at the html that was edited out of the question, you probably want one of the last two examples.

2
  • 1
    Your first example contains three apostrophes. I think you mean (//label[contains(.,'Missing Applicant\'s Signature')])[1] May 1, 2018 at 19:09
  • @BillHileman Well spotted, thanks! I've changed my example to just look for partial text May 2, 2018 at 9:58
1

xpath first element is locator[1], e.g. option[1]

so in your case perhaps

label[@for='517390_radio-button'][1]

generally I prefer css locators so that would be

label[for=517390_radio-button]:nth-child(2)

If you can't use that because the ID keeps changing you could use:

label[text="Missing Applicant's Signature"]:nth-child(2)
3
  • id is dynamic,so the number changes for each page load
    – RRR
    May 1, 2018 at 10:40
  • Your question title said the problem was two copies on the same page. the # changing is a different issue May 1, 2018 at 10:43
  • Updated answer to address that May 1, 2018 at 10:47
0

To select the first occurrence of the selector use the below line:

driver.findElementsByXpath("(//label[contains(text(), 'Missing Applicant')])[0]");

Where [0] denotes the 1st occurrence of the element.

0

On top of other good answers, for handling dynamic IDs the standard good practice is to use Xpath Axes where we find an parent node having static/unique property to reach to the child having dynamic IDs.

And the most important feature of this is , it's bi- directional (which is a feature of Xpath)which means it can be used by locating stable child to reach to an parent having dynamic ID as well.

Example:

Xpath=//*[text()='Enterprise Testing']//ancestor::div[1]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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