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 10,589 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
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,150 points
15,864 views
0 votes
2 answers

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,500 points
13,733 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,171 views
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,910 points
7,248 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,910 points
14,462 views
0 votes
2 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP