2

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">

enter image description here

Here's my attempt:

2 Answers 2

1

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:
    element.clear()
    element.send_keys(thelastmeter)

Let me know if you try this and how it works.

Edit: Also I should add that calling browser.implictly_wait() multiple times does nothing. The functions tells the webdriver to wait up to the specified amount of time when trying to find something before it moves on. It is something that just needs to be set once and then you don't have to call it again unless you want to change the amount of time that is waited.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried this but it didn't work. It actually didn't do anything...
I realized a mistake I made in my code, if you want to take a look at my changes and try again. The clear() and send_keys() function calls should have been on each element that was returned from the find_elements_by_xpath() call. The code I had there was trying to find an element again. Sorry about that.
0

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.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.