nerak99
nerak99

Reputation: 660

Selenium get table contents in Python3

I want to pick up all the hrefs in a table using Selenium (Chrome Driver)

I have a table of items where each table row looks like.

<tr>
    <td>
      <a href="a html page reference" class="itemName">
         <img data-script="item_image" data-widget="item-image" data-item="i165187" 
              data-size="70x70"  class="img" 
              src="html://source of image" alt="">The item name</a>
    </td>
    <td class="hide-s">Kitchen</td>
    <td class="hide-s">
      <span class="flag UK"></span>
      <span class="itemCountry">UK</span>
    </td>
</tr>

I have been playing around with various selenium methods with the essential problem (I think) being that there is no "name" associated with the href.

For example in the following I am hoping to get 'links' to contain the row so that I can pick out the hrefs.

links = []

links = driver.find_elements_by_class_name('itemName')

for link in links:
    link_urls_list.append(link.text)

Upvotes: 1

Views: 104

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193348

As per the HTML you have shared to collect all the hrefs you can use the following solution:

link_urls_list = []
links = driver.find_elements_by_xpath("//tr//td/a[@class='itemName']")
for link in links:
    link_urls_list.append(link.get_attribute("href"))

Upvotes: 1

Related Questions