This is the code iam using to count number of rows, it is returning only 1:
tablen = driver.find_elements(By.XPATH, ‘//*[@id=”m_mc_s0_z0_C_ctl00_tblForecast”]/tbody/tr’)
tblength = len(tablen)
print(tblength)
Result : it is printing output as 1, but their are many rows
<table id="m_mc_s0_z0_C_ctl00_tblForecast"> <tbody><tr> <td class="separatorLabel" colspan="15">Quarter 4 2021</td> </tr><tr> <td align="right" style="width:180px;">2021 November</td><td class="nonrequiredblock"></td><td class="ctl" style="width:160px;"><span id="m_mc_s0_z0_C_ctl00_c202111_wrapper" class="riSingle RadInput RadInput_Silk" style="width:80px;"><input id="m_mc_s0_z0_C_ctl00_c202111" name="m$mc$s0$z0$C$ctl00$c202111" class="riTextBox riEnabled" value="0.00" type="text"><input id="m_mc_s0_z0_C_ctl00_c202111_ClientState" name="m_mc_s0_z0_C_ctl00_c202111_ClientState" type="hidden" autocomplete="off" value="{"enabled":true,"emptyMessage":"","validationText":"0","valueAsString":"0","minValue":0,"maxValue":70368744177664,"lastSetTextBoxValue":"0.00"}"></span></td><td align="right" style="width:180px;">2021 December</td><td class="nonrequiredblock"></td><td class="ctl" style="width:160px;"><span id="m_mc_s0_z0_C_ctl00_c202112_wrapper" class="riSingle RadInput RadInput_Silk" style="width:80px;"><input id="m_mc_s0_z0_C_ctl00_c202112" name="m$mc$s0$z0$C$ctl00$c202112" class="riTextBox riEnabled" value="0.00" type="text"><input id="m_mc_s0_z0_C_ctl00_c202112_ClientState" name="m_mc_s0_z0_C_ctl00_c202112_ClientState" type="hidden" autocomplete="off" value="{"enabled":true,"emptyMessage":"","validationText":"0","valueAsString":"0","minValue":0,"maxValue":70368744177664,"lastSetTextBoxValue":"0.00"}"></span></td> </tr><tr> <td class="separatorLabel" colspan="15">Quarter 1 2022</td> </tr><tr> <td align="right" style="width:180px;">2022 January</td><td class="nonrequiredblock"></td><td class="ctl" style="width:160px;"><span id="m_mc_s0_z0_C_ctl00_c202201_wrapper" class="riSingle RadInput RadInput_Silk" style="width:80px;"><input id="m_mc_s0_z0_C_ctl00_c202201" name="m$mc$s0$z0$C$ctl00$c202201" class="riTextBox riEnabled" value="0.00" type="text"><input id="m_mc_s0_z0_C_ctl00_c202201_ClientState" name="m_mc_s0_z0_C_ctl00_c202201_ClientState" type="hidden" autocomplete="off" value="{"enabled":true,"emptyMessage":"","validationText":"0","valueAsString":"0","minValue":0,"maxValue":70368744177664,"lastSetTextBoxValue":"0.00"}"></span></td><td align="right" style="width:180px;">2022 February</td><td class="nonrequiredblock"></td><td class="ctl" style="width:160px;"><span id="m_mc_s0_z0_C_ctl00_c202202_wrapper" class="riSingle RadInput RadInput_Silk" style="width:80px;"><input id="m_mc_s0_z0_C_ctl00_c202202" name="m$mc$s0$z0$C$ctl00$c202202" class="riTextBox riEnabled" value="0.00" type="text"><input id="m_mc_s0_z0_C_ctl00_c202202_ClientState" name="m_mc_s0_z0_C_ctl00_c202202_ClientState" type="hidden" autocomplete="off" value="{"enabled":true,"emptyMessage":"","validationText":"0","valueAsString":"0","minValue":0,"maxValue":70368744177664,"lastSetTextBoxValue":"0.00"}"></span></td> </tr> </tbody></table>
Advertisement
Answer
Writing here in answer section as I have a snapshot to paste. Is your locator correct? I built hmtl of your code and inspected DOM, and although your locator shows 4 element length, they doesn’t seem to be the right ones. See the snapshot here, DOM snapshot The locator highlights to a row with which you cannot do anything much useful. I have written some code block. Please check if this would be useful to you.
driver.get("url here") time.sleep(3) x = driver.find_elements(By.XPATH, "//*[@id='m_mc_s0_z0_C_ctl00_tblForecast']//tbody//tr") print(f"Length of the element x is: {len(x)}") date_txt = driver.find_elements(By.XPATH, "//*[@class='ctl']//../td[@align='right']") print(f"lenght of date_txt is {len(date_txt)}") ls=[i.text for i in date_txt] print(ls) ctl = driver.find_elements(By.XPATH, "//*[@class='ctl']//input[@type='text']") print(f"length of ctl element is: {len(ctl)}") for ele in ctl: ele.clear() ele.send_keys("1.00") time.sleep(1) print(ele.get_attribute('value'))
Output:
Length of the element x is: 4 lenght of date_txt is 4 ['2021 November', '2021 December', '2022 January', '2022 February'] length of ctl element is: 4 1.00 1.00 1.00 1.00 Process finished with exit code 0