1

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.

1 Answer 1

1

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)
    
Sign up to request clarification or add additional context in comments.

5 Comments

Didn't work out :( row=[td.text for td in tr.find_elements_by_tag("td")] AttributeError: 'WebElement' object has no attribute 'find_elements_by_tag'
It worked out perfectly :) love that, many thanks Arundeep. I think I should put the code of excel somewhere in for loop, right?
If you want to just add it to excel the print row would be replaced with a write to excel.
I spent weeks to handle this. Thanks a lot really
No problem was nothing much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.