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,220 points
1,783 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,130 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 in Selenium by Anvi
• 12,860 points
146 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,090 points
592 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,130 points
901 views
0 votes
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,130 points
2,292 views
0 votes
2 answers

How to get HTML Source of WebElement in Selenium WebDriver using Python?

You can read innerHTML attribute to get source of ...READ MORE

answered Jun 13, 2018 in Selenium by Samarpit
• 5,130 points
13,985 views