1
<div class="col-xs-6 form-field input_controls">
<select name="ni.VE402765a6db1fef4043fb7d668c961997" id="ni.VE402765a6db1fef4043fb7d668c961997" class="form-control cat_item_option" onchange="if (typeof(variableOnChange) == 'function') variableOnChange('ni.VE402765a6db1fef4043fb7d668c961997')" aria-readonly="false" aria-required="true"><option value="" selected="SELECTED">-- None --</option><option value="Yes">Yes</option></select>
<input type="HIDDEN" class="cat_item_option" name="sys_original.ni.VE402765a6db1fef4043fb7d668c961997" id="sys_original.ni.VE402765a6db1fef4043fb7d668c961997" value=""></div>

The above is the DIV class of the "SelectBox". The numbers are changing for each and every "SelectBox". I dont want to hard code the id or name, i need a way to identify each and every SelectBox individually. All the "SelectBox" have the same name and id except for the numbers.

Ijaazops
  • 93
  • 1
  • 9

2 Answers2

1

Please use the class of the select tag.It should work.I have tried using python code.If you are using java Xpath remain same.

select=Select(driver.find_element_by_xpath("(//select[@class='form-control cat_item_option'])[n]"))
select.select_by_value('Yes')

where n=1.2..n

if you want to search 1st element put 1 instaed of n Let me Know if this work for you.

KunduK
  • 32,888
  • 5
  • 17
  • 41
  • 1
    The problem here is that there are several Select Box with the same name and i am unable to identify each Select Box uniquely. – Ijaazops Feb 08 '19 at 09:56
  • Try with index.I have updated my code.where 1 is the first element.that way you can identify other object. – KunduK Feb 08 '19 at 10:05
  • Using indexes on a collection is really not a good idea. Imagine one day another `SELECT` is added to the top of the form. Now your code is finding the wrong `SELECT` and it's going to take you a while to figure out what happened. – JeffC Feb 08 '19 at 14:20
0

If you don't have a unique identifier for the select you'll have to try to nail it down with the surrounding elements and possibly relying on the fixed ordinal of child elements for the containing element or so.

See Get Nth child of a node using xpath for more details on that.

JussiV
  • 178
  • 3
  • 15
  • I am trying to identify the Select Box not the elements within it. As u suggested in that case the Select Box below this also have the same value – Ijaazops Feb 08 '19 at 09:43
  • @ijaazops Surely there is some kind of structure outside of the div you included in the code sample? You should be able to find the correct element with a combination of class selectors, index selectors and element hierarchy. If not then you can get it. Depending on how dynamic the content is this might also be the case but I doubt it. – JussiV Feb 09 '19 at 10:07