0

My goal is to access a list of links (located in "event?id=85042").

<tr class="gvrow"> == $0
  <td>...</td>
  <td>
    <a href="event?id=85042"
                                text
                              </a>
  </td>

  ...

This repeats too, so I want to get a list of the links located in event?id=... I've tried this approach from this question Python Selenium - get href value but it is returning a list full of None

primary_links = driver.find_elements_by_class_name('gvrow')
links = [elem.get_attribute('href') for elem in primary_links]
print(links)

Output:

[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]

Desired Output:

['https://www.iccf.com/event?id=85042', 'https://www.iccf.com/event?id=79897', 'https://www.iccf.com/event?id=66745', ...]

How would I do it then?

Here is a better representation of the html enter image description here

1 Answers1

1

With the following line of code you will get a list containing the <td> element and any other element having class=gvrow:

driver.find_elements_by_class_name('gvrow')

Obviously, the <td> element(s) you get back do not have the href attribute.

Instead of your approach, you could use another method the get the link:

a_elements = driver.find_elements_by_xpath("//tr[@class='gvrow']/td/a")
links = [a_element.get_attribute('href') for a_element in a_elements]
AnsFourtyTwo
  • 2,480
  • 2
  • 13
  • 33