meto
meto

Reputation: 65

Iterating through table rows text with Python Selenium

Trying to iterate each row to create list for all rows separately. HTML table is;

<html>
<head>
<style>
table, th, td {
  border: 1px solid black;
}
</style>
</head>
<body>

<table>
  <tr>
    <th>Name</th>
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>John</td>
    <td>January</td>
    <td>$90</td>
  </tr>
  <tr>
    <td>Smith</td>
    <td>February</td>
    <td>$80</td>
  </tr>
   <tr>
    <td>Ken</td>
    <td>March</td>
    <td>$70</td>
  </tr>
   <tr>
    <td>Dan</td>
    <td>April</td>
    <td>$60</td>
  </tr>
</table>

</body>
</html>

And my python code is:

driver.get("file:///C:/HTML/new.html")
header = driver.find_elements_by_xpath("/html/body/table/tbody/tr[1]/th")
row = driver.find_elements_by_xpath("/html/body/table/tbody/tr[2]/td")
head = len(header) # 3
r1 = len(row) # 5
headers = []
rows = []
for h in header:
    headers.append(h.text)
for r in row:
    rows.append(r.text)
print (headers)
print (rows)

Result:

['Name', 'Month', 'Savings']
['John', 'January', '$90']

Header part (["Name","Month","Savings"]) works as it should be but I couldn't build up for loop to create list for all rows separately. Tried few diff for loops but all turned up to be a nested for loop.. What I want to do is actually as follows to write excel file later on:

["Name","Month","Savings"]
['John','January','$90']
['Smith','February','$80']
['John','March','$70']
['John','January','$60']

I would be very happy if you guys could give advice. Many thanks in advance.

Upvotes: 1

Views: 212

Answers (1)

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

To simply loop through the other trs and get all their td text you could do the following.

trs = driver.find_elements_by_xpath("//table//tr")
for tr in trs[1:]:
    row=[td.text for td in tr.find_elements_by_tag_name("td")]
    print(row)
    

Upvotes: 1

Related Questions