-1

I have on a web page a [Add Field] button, and when pressed, it will then display a single select list (drop-down menu).

The xpath of this single select drop-down list is: '//*[@id="adv_options[3][]'

With the second array item being empty l like '[]'.

I have code that will select the correct item on the first 'Add Field', but if I click 'Add Field' again, it displays another single select drop-down menu (with the same options), and the xpath of the second one is the same: '//*[@id="adv_options[3][]'

and so now if I try to select from that menu using that xpath, it will select the first one, and not the second one.

The site may be poorly coded, how would I select the correct item from the 2nd, 3rd, or forth single select drop-down menu option?


Update:

The following works to select the first item from the first drop down:

element = self.driver.find_element(By.XPATH, '//*starts-with(@id, 'adv_options[3][]')
select=Select(element)
select.select_by_value('1506')

Now there is a second drop down with the same id (adv_options[3][]), that I cannot find by xpath and select items from.


If anyone has encountered this and found code that would work, please share.

user10664542
  • 1,106
  • 1
  • 23
  • 43

1 Answers1

0

I expect there is another XPath you can use to uniquely determine the two drop-down boxes.

An XPath is the path to finding the element on the page. At the moment it is simply finding any nodes that have the id adv_options which I'm guessing both drop-down boxes have. If you want someone to find another XPath to uniquely determine the second box, you'll have to provide some of the HTML that contains the two boxes.

However, in selenium, you could do the following:

drop-down = driver.find_elements_by_xpath('//*[@id="adv_options[3][]')
#Returns an array of elements with this xpath
#E.g 
drop_down[0] #will be the first element with that path presumably the drop down box.
drop_down[1] #should be the second element with that path
Peter
  • 848
  • 8
  • 14