如何遍历所有tr以在td中给出值。在我的代码中,它覆盖了相同的tr / td。
我的桌子。
#qty to add
<tbody id="gridview-1161-body">
<tr id="gridview-1161-record-19842148" data-boundview="gridview-1161" class="x-grid-row x-grid-data-row" tabindex="-1">
<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>
<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%;">
</div></td>
</tr>
same like
<tr>..</tr></tbody>
所有ID均通过代码动态生成。 我的python代码:
#add qty
rowCount=len(driver.find_elements_by_xpath("//tbody[@id='gridview-1161-body']/tr"));
print(rowCount)
for row in rowCount:
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()
time.sleep(2)
#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)
由于动态id我不能给find_element(By.ID)所以我正在使用CSS_SELECTOR来找到td,但是它覆盖了相同的td ..如何给tr.next遍历表中的所有tr? / p>
答案 0 :(得分:1)
处理动态ID引发WebDriverWait()
和visibility_of_all_elements_located
()
和以下XPATH
选项。
driver=webdriver.Chrome()
rows=WebDriverWait(driver,15).until(EC.visibility_of_all_elements_located((By.XPATH,"//tbody[contains(@id,'-body')]//tr[@class='x-grid-row x-grid-data-row']")))
for rownum in range(len(rows)):
#To avoid stale exception re-assign rows elements again
rows = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//tbody[contains(@id,'-body')]//tr[@class='x-grid-row x-grid-data-row']")))
element=rows[rownum].find_element_by_xpath(".//td[contains(@class,'rp-grid-editable-cell rp-grid-editable-cell') and @role='gridcell']")
element.click()
input=rows[rownum].find_element_by_xpath(".//input[@name='Quantity' and @role='spinbutton']")
input.send_keys('10')
input.send_keys(Keys.ENTER)
答案 1 :(得分:0)
获取所有行,然后找到子td
和input
:
wait = WebDriverWait(driver, 20)
rows = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "tr[id*='-record-'].x-grid-data-row")))
for row in rows:
row.find_element_by_css_selector("td[role='gridcell']").click()
row.find_element_by_name("Quantity").send_keys("10", Keys.ENTER)
xpath和索引的第二种方式:
wait = WebDriverWait(driver, 10)
row_locator = "(//tr[contains(@id,'-record-')])"
rows_len = len(wait.until(EC.presence_of_all_elements_located((By.XPATH, row_locator))))
for i in range(1, rows_len + 1):
wait.until(EC.element_to_be_clickable((By.XPATH, f"{row_locator}[{i}]/td[@role='gridcell']"))).click()
driver.find_element_by_xpath(f"{row_locator}[{i}]/input[@name='Quantity']").send_keys("10", Keys.ENTER)
答案 2 :(得分:0)
要遍历祖先的<input>
标签中的所有子<tr>
标签以发送字符序列,您需要为{产生一个
visibility_of_all_elements_located()