5

I am trying the below xpath for Label, but I'm not able to locate the element.

driver.findElement(By.xpath("//div[label[contains(text(),'Patient's Name']]")).isEnabled();

XPath: .//*[@id='update_patient_profile']/div/div[1]/label ---Taken from FirePath.

Below is the HTML source for the field.

<form id="update_patient_profile" action="/subscriber/" method="post" name="update_patient_profile"> 
  <div class="subscriberAddPatient"> 
    <div class="formData nameInputs"> 
  <label for="first_name">Patient's Name</label>
  <input id="first_name" class="left nameRule" type="text" onblur="resetTxtAdd($(this))" onfocus="emptyFieldAdd($(this))" onclick="emptyFieldAdd($(this))" name="first_name" value="First Name" maxlength="24"/>

Can anyone suggest me the correct XPath for the Label.

Bharat Mane
  • 296
  • 2
  • 12
  • 22
pratik
  • 59
  • 2
  • 3
  • 11
  • Your HTML source is broken, please check that. Have a look at the [FAQ] regarding formatting your questions. – Jens Erat Sep 24 '13 at 18:21

3 Answers3

5

You should use the below XPATH

 //*[@id='update_patient_profile']//div[2]/label[.='Patient's Name']
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
2

One can also use text() instead of dot ".". To verify the label text one can use this path too

//form[@id='update_patient_profile']//div[2]/label[text()='Patient's Name']
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
sndp
  • 21
  • 3
1

//form[@id='update_patient_profile']/label[text()='Patient's Name']

As the label tag is also a child of form tag.

zishan paya
  • 503
  • 1
  • 6
  • 29