Home > Net >  Selenium multiple input fields
Selenium multiple input fields

Time:07-16

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  data-json-object- 
   name="Building">
    <div >
        <div >
            <span ></span>
        </div>
        <div >
            <div >
                
                <label >Building NO</label>
                <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"  value=" ">
                <div ></div>
            </div>
        </div>
        <div >
            <div >
                <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" >
            </div>
        </div>
    </div>
</li>
<li  data-json-object-name="Building">
    <div >
        <div >
            <span ></span>
        </div>
        <div >
            <div >
                
                <label >Building NO</label>
                <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"  value=" ">
                <div ></div>
            </div>
        </div>
        <div >
            <div >
       

     <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])

CodePudding user response:

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.

CodePudding user response:

for x in buil:
   driver.find_elements(By.XPATH,"//input[@class='text form-control js-checkout-field-validation js-checkout-json-object-field js-sku-input-field generic-js-site-search-input']")[-1].send_keys(k)

Could always just find elements and access the last 1 with -1 in Python.

  • Related