0

How Do I get the length of all the elements inside this particular BODY?

XPATH = //*[@id="createEdit"]/div/div/table/tbody

<tbody data-v-97081d06="">
    <tr data-v-97081d06="" gogo-test="data_table_with_selection_row_0" tabindex="0" class="t-body--sansSerif">
    <tr data-v-97081d06="" gogo-test="data_table_with_selection_row_1" tabindex="0" class="t-body--sansSerif">
    <tr data-v-97081d06="" gogo-test="data_table_with_selection_row_2" tabindex="0" class="t-body--sansSerif">
</tbody>

There is a total of 3 elements inside it and I want to get the length of it from the main element.

Sofi
  • 498
  • 4
  • 12

2 Answers2

1

Ideally, instead of getting the length of all the elements inside the <body> tag you may like to get the length of all the <tr> elements within the <body>.

To get the length of the <tr> elements you can use either of the following Locator Strategies:

  • Using css_selector:

    print(len(driver.find_element(By.CSS_SELECTOR, "#createEdit > div > div > table > tbody tr")))
    
  • Using xpath:

    print(len(driver.find_element(By.XPATH, "//*[@id="createEdit"]/div/div/table/tbody//tr")))    
    

Ideally you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(len(WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "#createEdit > div > div > table > tbody tr")))))
    
  • Using XPATH:

    print(len(WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[@id="createEdit"]/div/div/table/tbody//tr")))))
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

To count child elements you can do

tbody = driver.find_element_by_xpath('//*[@id="createEdit"]/div/div/table/tbody')
print(len(tbody.find_elements_by_xpath('./*')))
JaSON
  • 4,843
  • 2
  • 8
  • 15