0

I am new to XPath and need some help.

The system auto generates the id which looks something like this:

<input type="file" class="form-file" size="22" 
       name="files[entry-23245_field_entry_attachment_und_0]" 
       id="edit-entry-23245-field-entry-attachment-und-0-upload" 
       style="background-color: transparent;">

I am able to locate the id using xpath or css however the numbers within the id string changes as this is randomly generated so the next time my test runs, it fails because it cant locate the string.

I would like to know if it is at all possible to write an xpath expression that will look for everything from the start of the string edit-entry- then some how look for any integer value between 0-9 within that string -23245-, then also match the end part field-entry-attachment-und-0-upload. this way when my test runs, it is able to locate the element all the time even if the numbers within the string change. iv tried adding \d+ to my xpath but it doesn't seem to pick it up.

This is the xpath:

//*[@id="edit-entry-23245-field-entry-attachment-und-0-upload"]
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

That is because your Xpath isn't extracting the right attribute. Use an Xpath like this to get the id of the element:

//input[@type="file" and @class="form-file"]/@id

This Regex should extract the value you are looking for:

/edit-entry-\d+-field-entry-attachment-und-0-upload/

If \d+ doesn't work for you this is another possibility:

/edit-entry-[0-9]+-field-entry-attachment-und-0-upload/
Severin
  • 8,508
  • 14
  • 68
  • 117
  • thanks Severin. can use regex with the xpath construct? Please could show me how this will be written within the xpath? – user2240134 Feb 27 '14 at 12:08
  • Its not possible to do that. You have you extract the id and match it with the provided regex. – Severin Feb 27 '14 at 12:12
  • thanks again. i was reading this post (http://stackoverflow.com/questions/405060/can-i-use-a-regex-in-an-xpath-expression) and thought its possible to user "contains" in xpath so thought it might be possible to say something like @id contains "[0-9]". or am i getting confused? – user2240134 Feb 27 '14 at 12:20
  • This example only seems to work if you are using .NET – Severin Feb 27 '14 at 12:23
0

Just an idea for workaround, since we can't use regex in pure XPath.

In case you just need to match <input> element with id equals "edit-entry-[arbitrary-characters-here]-field-entry-attachment-und-0-upload", we can use starts-with() and ends-with() functions like this :

//*
    [starts-with(@id, "edit-entry-") 
        and 
     ends-with(@id, "-field-entry-attachment-und-0-upload")]

and in case you're using XPath 1.0 where ends-with() function is not available :

//*
    [starts-with(@id, "edit-entry-") 
        and 
     (
        "-field-entry-attachment-und-0-upload" 
            = 
          substring(@id, string-length(@id) - string-length("-field-entry-attachment-und-0-upload") +1)
     )
    ]
Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137