3

I tried to find the XPath of a text field username and password, but it keeps on changing dynamically. I won't be able to use starts-with or contains in a findelement using XPath. Here's the HTML DOM-

      <div id="contents">
          <h1 style="white-space:pre; width:80px; float:left;line-height:35px;">Login</h1>
          <span style="float:left; padding-top:13px; COLOR:#990033; font-weight:bold;">  Student | Parent | Faculty</span>
          <div class="form-elements">
          <div class="form-elements">
          <div class="form-elements">
              <div class="label-txt">Password</div>
              <input id="rcnr2uew1m0rkikeaaniwk" type="password" style="display:none;" name="rcnr2uew1m0rkikeaaniwk"/>
              <input id="ko2xs123ebqyoluh15bulu" type="password" style="display:none;" name="ko2xs123ebqyoluh15bulu"/>
              <input id="cuouek4bfz41etm4hroj0r" type="password" style="display:none;" name="cuouek4bfz41etm4hroj0r"/>
              <input id="u2ta3gv2o2ce0azx5plpuh" type="password" name="u2ta3gv2o2ce0azx5plpuh"/>
              <input id="g03nwjuzhqnkuwgsl4q2mu" type="password" style="display:none;" name="g03nwjuzhqnkuwgsl4q2mu"/>
              <input id="gddwv4z3amojk0yvoxi2v4" type="password" style="display:none;" name="gddwv4z3amojk0yvoxi2v4"/>
              <input id="kxecmkho2vf1vcfb42icjr" type="password" style="display:none;" name="kxecmkho2vf1vcfb42icjr"/>
              <span id="ctl04" style="color:Red;visibility:hidden;">*</span>
         </div>

I tried to find the input[4] with no style.

Absolute XPath: html/body/form/div[3]/div[1]/div[2]/div[1]/div[1]/div[1]/div[2]/input[4]

Next time how it changes-

Absolute XPath: html/body/form/div[3]/div[1]/div[2]/div[1]/div[1]/div[1]/div[2]/input[17]

id and name of the input also keeps on changing without any common trait

4

For the password you could try:

//input[@type='password'][not(@style)]

For the username :

//input[@type='text'][not(@style)]

This will select all input elements where the type attribute has the value 'password' (or 'text'), that do not have the style attribute defined.

Ofcourse these are not super precise yet, you could additionally check whether there is a sibling div element that has the value 'Password' or 'User Name'

//div[text()='User Name']/../input[@type='text'][not(@style)]
//div[text()='Password']/../input[@type='password'][not(@style)]
  • Thanks, if style attribute is present then what could be the approach – bot13 Feb 4 '17 at 17:31
  • Assuming you only want to match input elements that do not have display:none on the style attribute, this should work //div[text()='Password']/../input[@type='password'][not(contains(@style,'display:none'))] – jpjwolli Feb 4 '17 at 19:16
-1
<a class="viewEmailBtn">
 <span class="viewEmailLabel" style="display: none;">SHOW EMAIL ADDRESS</span>
 <span class="viewEmail" style="display: inline;">customer_care@ablerex.com.sg</span>
</a>

Code:

$company_email = $xpath1->query("//*[@class='viewEmailBtn']/span[2][(contains(@style,'display:none'))]");
  • I've downvoted your answer as this question already has an accepted answer from almost two years ago so it's not particularly useful. – trashpanda Nov 14 '18 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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