Unable to locate element for LABEL with the XPath expression

妖精的绣舞 提交于 2021-02-08 01:54:21

问题


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.


回答1:


You should use the below XPATH

 //*[@id='update_patient_profile']//div[2]/label[.='Patient's Name']



回答2:


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

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




回答3:


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']


来源:https://stackoverflow.com/questions/18988157/unable-to-locate-element-for-label-with-the-xpath-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!