4

I have to deal with a table like this :

<table id="product">
  <tr>
    <td data-sortable-id="ax01">Axe</td>
    <td>Wood</td>
    <td>Red</td>
  </tr>
  <tr>
    <td data-sortable-id="ha01">Hammer</td>
    <td>Iron</td>
    <td>Black</td>
  </tr>
  <tr>
    <td data-sortable-id="na01">Nail</td>
    <td>Metal</td>
    <td>Black</td>
  </tr>
  <tr>
    <td colspan="3">3 Products Listed</td>
  </tr>
</table>

How can I select all td elements which have the attribute data-sortable-id? I can't find anything in Google search about finding elements by attribute (without a value, attribute's own name only).

I've tried something like

x = webdriver.find_elements_by_xpath("//td[@data-sortable-id]")
print(len(x))

but the result is 0.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

3

To find all the <td> tags which have attribute data-sortable-id you can use the following granular xpath :

"//table[@id='product']/tr//td[@data-sortable-id]"

Perhaps you need to induce WebDriverWait in-conjunction with expected_conditions clause as visibility_of_all_elements_located as follows :

x = WebDriverWait(webdriver, 30).until(EC.visibility_of_all_elements_located((By.XPATH, "//table[@id='product']//tr//td[@data-sortable-id]")))
print(len(x))
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352