Selenium multiple input fields

Issue

I am stuck on how to enter the input value selecting a building. Initially, there will be only two empty input fields for selecting a building, as you enter one it will add an empty input field. I don’t see any indexing on how to select and enter the building names one by one without overwriting them, the building values are
buil= ["HillsDale","astoria","Franklin",….]
here is what the HTML looks like

 <li class="checkout-order-summary-list-added-item js-checkout-added-item" data-json-object- 
   name="Building">
    <div class="row">
        <div class="item-valid-icon hidden">
            <span class="glyphicon glyphicon-ok"></span>
        </div>
        <div class="col-xs-7 col-sm-7 col-md-6">
            <div class="form-group js-sku-container">
                
                <label class="control-label ">Building NO</label>
                <input class="text form-control js-checkout-field-validation js-checkout-json-object-field js-sku-input-field generic-js-site-search-input ui-autocomplete-input" type="text" name="BuildingsSKU" data-validation-reason="Buildings are added to order." data-json-object-name="Buildings" data-update-targets=".subtotals-parts,.subtotals" >
                <input type="hidden" class="js-hidden-sku-field" value=" ">
                <div class="js-validation-container help-block"></div>
            </div>
        </div>
        <div class="col-xs-5 col-sm-5 col-md-2">
            <div class="form-group">
                <label class="control-label">Qty</label>       
    
<input type="number" min="1" max="999" data-range-validation-error-msg="Value should be in range from 1 to 999" value="1" data-toggle="tooltip" qty="" name="quantity" class="text form-control js-checkout-json-object-field js-checkout-qty ">
            </div>
        </div>
    </div>
</li>
<li class="checkout-order-summary-list-added-item js-checkout-added-item" data-json-object-name="Building">
    <div class="row">
        <div class="item-valid-icon hidden">
            <span class="glyphicon glyphicon-ok"></span>
        </div>
        <div class="col-xs-7 col-sm-7 col-md-6">
            <div class="form-group js-sku-container">
                
                <label class="control-label ">Building NO</label>
                <input class="text form-control js-checkout-field-validation js-checkout-json-object-field js-sku-input-field generic-js-site-search-input" type="text" name="BuildingsSKU" data-validation-reason="Buildings were added to order." data-json-object-name="Buildings" data-update-targets=".subtotals-parts,.subtotals" >
                <input type="hidden" class="js-hidden-sku-field" value=" ">
                <div class="js-validation-container help-block"></div>
            </div>
        </div>
        <div class="col-xs-5 col-sm-5 col-md-2">
            <div class="form-group">
       

     <label class="control-label ">Qty</label>

and this is what the form looks like

enter image description here

Any help really be appreciated.
so far I have tried
driver.find_element_by_xpath("//div[contains(@class,’text form-control js-checkout-field-validation’)]").send_keys(buil[0])

Solution

Something to try. Get all building inputs and index them to send text:

fields = driver.find_elements_by_xpath("//div[contains(@class,'text form-control js-checkout-field-validation')]")
    
    fields[0].send_keys(buil[0])
    fields[1].send_keys(buil[1])

Or you can just use xpath directly:

driver.find_element_by_xpath("(//div[contains(@class,'text form-control js-checkout-field-validation')])[1]").send_keys(buil[0])

driver.find_element_by_xpath("(//div[contains(@class,'text form-control js-checkout-field-validation')])[2]").send_keys(buil[1])

I can’t say if this is the correct xpath since I don’t have the url but it should be close.

Answered By – JD308

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published