0

I've a table who regroups multiple data-label with multiple names , I tried to use driver.get_elements_by_css_selector method but it didn't work , here's the HTML that I wanna get values from:

<tbody>
 <tr>
   <td data-label="Player">Pierre-Emerick Aubameyang</td>
   <td data-label="Gross Weekly Wages">£250,000</td>
  </tr>
 <tr>
   <td data-label="Player">Willian</td>
   <td data-label="Gross Weekly Wages">£195,000</td>
 </tr>
  .
  .
  .
</tbody>

Since I'm having a lot of tr where each one has player information , I did this code but it's not working.

elements = driver.find_elements(By.TAG_NAME, 'tr')
        for element in elements:
            player_info = element.find_elements(By.TAG_NAME, 'td')
            player_name = player_info[0].get_attribute('data-label')
            player_week_salary = player_info[1].get_attribute('data-label')
  • 1
    Does this answer your question? [How to get attribute of element from Selenium?](https://stackoverflow.com/questions/30324760/how-to-get-attribute-of-element-from-selenium) – Mike Scotty Mar 17 '21 at 15:56
  • I tired that but It didnt work , I didn't know how can I refer to the `data-label` , since I call it as a tag attribute but didn't provide me any information. –  Mar 17 '21 at 16:03
  • If you've tried that, you should include your code in your question, so we can help you fix it. – Mike Scotty Mar 17 '21 at 16:05
  • @MikeScotty sorry , I'm new here , yes I added that now , you can check sir. thank you –  Mar 17 '21 at 16:39
  • @testing01 try to use better: elements = driver.find_elements_by_xpath('//tr/td') for el in elements: print(el.get_attribute('data-label')) – Vova Mar 17 '21 at 16:40
  • 1
    @testing01 : is that only html you have?? Can post the entire table HTML? – KunduK Mar 17 '21 at 16:40
  • I did that , thank you for your help sir –  Mar 17 '21 at 16:44
  • @testing01 : What error are you getting? while printing those value? – KunduK Mar 17 '21 at 16:53
  • it gets me an empty list –  Mar 17 '21 at 17:00
  • `player_name = player_info[0].get_attribute('data-label')` `IndexError: list index out of range` –  Mar 17 '21 at 17:01
  • @testing01 : I believe you want to get the players name and weekly wages?? in that case `print[ name.text for name in driver.find_elements(By.XPATH , '//td[@data-label="Player"]')]` and `print[ wages.text for wages in driver.find_elements(By.XPATH , '//td[@data-label="Gross Weekly Wages"]')]` – KunduK Mar 17 '21 at 17:47

3 Answers3

1

to get data by attribute in selenium, use method:

element.get_attribute('you_attribute')

for that example it should look like it:

elements = driver.find_elements(By.TAG_NAME, 'td')
attr = elements[0].get_attribute('data-label')
print(attr)

or use already answered questions by link: How to get attribute of element from Selenium?

Vova
  • 3,117
  • 2
  • 15
  • 23
  • 1
    It seems like he has a problem with identifying locators. – vitaliis Mar 17 '21 at 16:30
  • I tried some modification on my code i'll add that to my original post , so I'm trying to get all informations. –  Mar 17 '21 at 16:35
  • 1
    @testing01 of course if you need to get all of the tr's data-labels, you can run the loop with elements like: for el in elements: print(el.get_attribute('data-label')) i just provided example – Vova Mar 17 '21 at 16:37
  • Vova, good point. Then you can use find_elements_by_xpath or find_elements_by_css_selector to put all your data in a list. – vitaliis Mar 17 '21 at 17:37
0

You need to correctly identify your locator. In your case XPATH locator is: //tr/td[1]

driver.find_element_by_xpath("//tr/td[1]")

And

driver.find_element_by_xpath("//tr/td[2]")

for the second element and so on.

You can also use CSS selectors. Read about nth-of-type(1) to identify similar locators. For taking attribute use get_attribute method. Try:

locator = driver.find_element_by_xpath("//tr/td[1]")
your_attribute = locator.get_attribute("data-label")

Or use td:nth-of-type(1), td: nth-of-type(2) and so on as CSS locators. It will look like this:

driver.find_element_by_css_selector("td:nth-of-type(1)").get_attribute("data-label")
vitaliis
  • 4,082
  • 5
  • 18
  • 40
0

To get the elements by xpath you'd have to . to the child element.

elements = driver.find_elements(By.TAG_NAME, 'tr')
    for element in elements:
        player_name = element.find_element_by_xpath("./td[1]").get_attribute('data-label')
        player_week_salary = element.find_element_by_xpath("./td[2]").get_attribute('data-label')
        print(player_name,player_week_salary)
Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32