我正在尝试为td标签内的所有输入框提供值。但是我的代码它只为第一个 td 提供价值。这里的 id 是通过代码动态创建的。
<tbody id="gridview-1161-body">
<tr id="gridview-1161-record-19832230" data-boundview="gridview-1161" data-recordid="19832230" data-recordindex="2" class="x-grid-row x-grid-data-row" tabindex="-1">
#supplier item
<td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-headerId-gridcolumn-1154" id="ext-gen2524">
<div class="x-grid-cell-inner " style="text-align:left;">
<div class="rp-invalid-cell rp-icon-alert-require-field"></div>xxx/div></td>
#id
<td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-headerId-gridcolumn-1156" id="ext-gen2526">
<div class="x-grid-cell-inner " style="text-align:left;">
<div class="rp-invalid-cell rp-icon-alert-require-field"></div>yy</div></td>
#cost
<td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-headerId-gridcolumn-1157" id="ext-gen2527">
<div class="x-grid-cell-inner " style="text-align:right;">
<div class="rp-invalid-cell rp-icon-alert-require-field"></div>$15.00</div></td>
#qty to add
<td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-headerId-gridcolumn-1158 rp-grid-editable-cell rp-grid-editable-cell" id="ext-gen2535">
<div class="x-grid-cell-inner " style="text-align:right;">
<div class="rp-invalid-cell rp-icon-alert-require-field">
</div> </div></td>
<td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-headerId-gridcolumn-1158 rp-grid-editable-cell rp-grid-editable-cell" id="ext-gen2542">
<div class="x-grid-cell-inner " style="text-align:right;">
<div class="rp-invalid-cell rp-icon-alert-require-field" id="ext-gen2768">
</div> </div></td>
<td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-headerId-gridcolumn-1158 rp-grid-editable-cell rp-grid-editable-cell" id="ext-gen2556">
<div class="x-grid-cell-inner " style="text-align:right;">
<div class="rp-invalid-cell rp-icon-alert-require-field">
</div> </div></td>
<td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-headerId-gridcolumn-1158 rp-grid-editable-cell rp-grid-editable-cell" id="ext-gen2563">
<div class="x-grid-cell-inner " style="text-align:right;">
<div class="rp-invalid-cell rp-icon-alert-require-field">
</div> </div></td>
输入框代码:每个TD都有以下相同的输入框(相同的ID,类名(
<input id="numberfield-1243-inputEl" type="text" role="spinbutton" name="Quantity" class="x-form-field x-form-text x-form-focus x-field-form-focus x-field-default-form-focus" autocomplete="off" style="width: 100%;">
蟒蛇代码:
#add qty
for count in range(items_tot):
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "td.x-grid-cell.x-grid-td.rp-grid-editable-cell[role='gridcell']")))
element.click()
#input box to give qty-working for this id
driver.find_element(By.ID, "numberfield-1243-inputEl").send_keys('10')
driver.find_element(By.ID, "numberfield-1243-inputEl").send_keys(Keys.ENTER)
首先你需要找到所有的输入元素。
通过使用"driver.findElements"函数
并做这样的动作。
elements = driver.find_elements_by_xpath('//table//tr//td//input') #xpath to find input
for element in elements:
element.send_keys('10')
element.send_keys(Keys.ENTER)