简体   繁体   中英

How to traverse all the tr elements with in the table using selenium python

how to traverse all tr to give values in td.In my code it is overriding same tr/td.

My table.

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

Here all the id's are dynamically generating via code. My python code:

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

due to dynamic id i can't give find_element(By.ID)So i am using CSS_SELECTOR to find the td,but it is overriding same td..how to give tr.next to traverse all tr in table ?

To handle dynamic ID Induce WebDriverWait() and visibility_of_all_elements_located () and Following XPATH option.

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) 

Get all rows, then find child td and 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)

Second way with xpath and index:

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)

To traverse all the child <input> tags with in the ancestor <tr> tags to send character sequence you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use the following Locator Strategies :

for element in WebDriverWait(driver, 30).until(EC.visibility_of_all_elements_located((By.XPATH, "//tbody[starts-with(@id, 'gridview') and contains(@id, '-body')]/tr/td//input[@name='Quantity' and starts-with(@id, 'numberfield-')]"))):
    element.send_keys('10')
    element.send_keys(Keys.ENTER)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM