I'm using Python and Selenium to fill out a web form. On one of the pages, it has multiple input fields with the same ID. Sometimes it's 1, 2, 3 and 4. Each ticket is different.
Here's my attempt:
how_many_meter_options = len(browser.find_elements_by_xpath("//span[contains(@class,'meterdisplay')]"))
browser.implicitly_wait(30)
print("There are ")
print(how_many_meter_options)
print(" Meter Options")
thecountofmeters = str(how_many_meter_options)
for row_of_meters in thecountofmeters:
browser.find_element_by_xpath("//span[contains(@class,'meterdisplay')]").click()
thelastmeter = browser.find_element_by_id("meteredit-lastreading-display").text
print(thelastmeter)
browser.implicitly_wait(30)
browser.find_element_by_id('meteredit-display').clear()
browser.find_element_by_id('meteredit-display').send_keys(thelastmeter)
browser.implicitly_wait(30)
browser.find_element_by_name('ok').click()
browser.implicitly_wait(30)
This only fills out the first input field. I need it to do all.
Here's the html
<input rt-autofocus="" type="number" id="meteredit-display" name="display" data-ng-model="meter.MeterDisplay" min="0" data-ng-disabled="!canEditMeter()" class="ng-pristine ng-valid ng-valid-number ng-valid-min ng-valid-pattern">
Here's my attempt:
CodePudding user response:
The function find_element_by_id()
will only find the first element with a matching id that you put in the arguments. To my knowledge there is no function to find multiple using just the id as an argument, however you might be able to use find_elements_by_xpath("//input[id='meteredit-display']")
which will return a group of elements you can iterate through and apply your commands on.
Something like this:
input_elements = browser.find_elements_by_xpath("//input[id='meteredit-display']")
for element in input_elements:
browser.implicitly_wait(30)
browser.find_element_by_id('meteredit-display').clear()
browser.find_element_by_id('meteredit-display').send_keys(thelastmeter)
browser.implicitly_wait(30)
Let me know if you try this and how it works.
CodePudding user response:
Like Nathan Roberts suggested above, you can look for the xpath, however, having multiple elements with the same id is not considered valid HTML. If you have any say in this I'd recommend requesting the change.
Another option would be to use a regular expression on the raw html.