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,330 points
5,015 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,870 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Installing Selenium Webdriver with Python package

Hey Hemant, for installing Selenium Webdriver with ...READ MORE

answered May 8, 2019 in Selenium by Anvi
• 14,050 points
1,949 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,510 points
2,850 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,870 points
2,212 views
+1 vote
1 answer

How to handle notifications in Python with Selenium (Chrome WebDriver)

Below will help you: You can disable the ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,870 points
5,265 views
0 votes
2 answers