How to find Elements by their Attribute in Python Selenium WebDriver

0 votes

I want to select all td elements which have the attribute data-sortable-id .

I've tried something like x = webdriver.find_elements_by_xpath("//td[@data-sortable-id]")

print(len(x))

but the result is 0.

Below is the table:

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


Jul 19, 2018 in Selenium by Martin
• 4,320 points
1 flag 11,357 views

1 answer to this question.

0 votes

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

answered Jul 19, 2018 by Samarpit
• 5,910 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
+3 votes
1 answer