0

I have couple of input in html. How can I write an xpath query with a wildcard character to replace the numeric value in the name attribute to select all input elements? I tried //div/input[contains(@name, "name")], it works, but I prefer to use a wildcard. Something like //div/input[@name="models[.*.].name"].

The html:

<div>
    <input type="text" name="models[1].name" value="">
    <input type="text" name="models[2].name" value="">
    <input type="text" name="models[3].name" value="">
    <input type="text" name="models[4].name" value="">
    <input type="text" name="models[5].name" value="">
    <input type="text" name="models[6].name" value="">
</div>
Marcel
  • 1,443
  • 2
  • 14
  • 24
jacobcan118
  • 7,797
  • 12
  • 50
  • 95

1 Answers1

0

Try this CssSelector

input[name^='models'][name$='.name']

This will get you all inputs that have a name which starts with models and ends with .name

There is also a way to do this in XPath, by using

//input[starts-with(@name, 'models') and ends-with(@name, '.name')]

But this is only supported by browsers using XPath 2.0, so this will not work in Chrome

The alternative is to use a Xpath 1.0 compatible way, which isn't really readable

//input[starts-with(@name, 'models') and substring(@name, string-length(@name) - string-length('name') +1) = 'name']
Marcel
  • 1,443
  • 2
  • 14
  • 24