0

I'm trying to generate a dynamic table and email that table with the help of python selenium.

With WebDriver, we were able to get the dynamic data of required fields. But I couldn't able to tabulate those data in a single table. I tried with email function in a loop.

TotalBugs = int(TotalBugs) + 1 
# BugList = [] 
for ID in range(1, TotalBugs): 
BugID = driver.find_element_by_xpath('/html[1]/body[1]/div[1]/section[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/issuetable-web-component[1]/table[1]/tbody[1]/tr['+ str(ID) +']/td[2]/a[1]').get_attribute('href') 
Summary = driver.find_element_by_xpath('/html[1]/body[1]/div[1]/section[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/issuetable-web-component[1]/table[1]/tbody[1]/tr['+ str(ID)+']/td[3]/p[1]').text 
Reporter = driver.find_element_by_xpath('/html[1]/body[1]/div[1]/section[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/issuetable-web-component[1]/table[1]/tbody[1]/tr['+str(ID)+']/td[5]/span[1]/a[1]').text 
Resolution = driver.find_element_by_xpath('/html[1]/body[1]/div[1]/section[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/issuetable-web-component[1]/table[1]/tbody[1]/tr['+str(ID)+']/td[8]').text 
UpdatedDate = driver.find_element_by_xpath('/html[1]/body[1]/div[1]/section[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/issuetable-web-component[1]/table[1]/tbody[1]/tr['+str(ID)+']/td[10]/span[1]/time[1]').text 
print(BugID, Summary, Reporter, Resolution, UpdatedDate) 
tabular = [(BugID, Summary, Reporter, Resolution, UpdatedDate)] 
  for BugID, Summary, Reporter, Resolution, UpdatedDate in tabular: 
   tabular = [(BugID, Summary, Reporter, Resolution, UpdatedDate)] 
   message = "<thead><tr><th>Bug ID</th><th>Summary</th><th>Reporter</th><th>Resolution</th><th>Updated Date</th></tr></thead><tbody><tr><td>"+BugID+"</td><td>"+Summary+"</td><td>"+Reporter+"</td><td>"+Resolution+"</td><td>"+UpdatedDate+"</td></tr></tbody>" 
   SERVER = "xyz.smtp.com" 
   me = "[email protected]" 
   you = "[email protected]" # must be a list 
   msg = MIMEMultipart('alternative') 
   msg['Subject'] = "Daily report" + " - " + str(currentdate()) 
   msg['From'] = me 
   msg['To'] = you 
   html = "<html> <body><p> Hi team</p><p>Please find the below mentioned tasks for today:</p><table class='table table-bordered ' border='1'>"+ message +"</table></body></html>" 
   # part1 = MIMEText(text, 'plain') 
   part2 = MIMEText(html, 'html') 
   # msg.attach(part1) 
   msg.attach(part2) 
   print(msg) 
   server = smtplib.SMTP(SERVER) 
   server.sendmail(me, you, msg.as_string()) 
   server.quit() 

With the above code I received the email as per how many rows in the table. But I need all those data in a single table like below in single email

Expected output :

| BugID | Summary | Reporter | Resolution | Updated Date |
|-------|---------|----------|------------|--------------|
| Bug1  | title1  | rep1     | res1       | 07/07/2019   |
| Bug2  | tit2    | rep2     | res2       | 08/08/2019   |
| Bug3  | tit3    | rep3     | res3       | 09/09/2019   |

3

1 Answer 1

2

Try this below logic.

message = "<thead><tr><th>Bug ID</th><th>Summary</th><th>Reporter</th><th>Resolution</th><th>Updated Date</th></tr></thead><tbody>"
TotalBugs = int(TotalBugs) + 1
# BugList = []
for ID in range(1, TotalBugs):
    BugID = driver.find_element_by_xpath('//issuetable-web-component[1]/table[1]/tbody[1]/tr['+ str(ID) +']/td[2]/a[1]').get_attribute('href')
    Summary = driver.find_element_by_xpath('//issuetable-web-component[1]/table[1]/tbody[1]/tr['+ str(ID)+']/td[3]/p[1]').text
    Reporter = driver.find_element_by_xpath('//issuetable-web-component[1]/table[1]/tbody[1]/tr['+str(ID)+']/td[5]/span[1]/a[1]').text
    Resolution = driver.find_element_by_xpath('//issuetable-web-component[1]/table[1]/tbody[1]/tr['+str(ID)+']/td[8]').text
    UpdatedDate = driver.find_element_by_xpath('//issuetable-web-component[1]/table[1]/tbody[1]/tr['+str(ID)+']/td[10]/span[1]/time[1]').text
    print(BugID, Summary, Reporter, Resolution, UpdatedDate)
    newRow = "< tr > < td > " + BugID + " < / td > < td > " + Summary + " < / td > < td > " + Reporter + " < / td > < td > " + Resolution+" < / td > < td > "+UpdatedDate+" < / td > < / tr >"
    message = message+newRow
message = message + "</tbody>"
SERVER = "xyz.smtp.com"
me = "[email protected]"
you = "[email protected]" # must be a list
msg = MIMEMultipart('alternative')
msg['Subject'] = "Daily report" + " - " + str(currentdate())
msg['From'] = me
msg['To'] = you
html = "<html> <body><p> Hi team</p><p>Please find the below mentioned tasks for today:</p><table class='table table-bordered ' border='1'>"+ message +"</table></body></html>"
# part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
# msg.attach(part1)
msg.attach(part2)
print(msg)
server = smtplib.SMTP(SERVER)
server.sendmail(me, you, msg.as_string())
server.quit()
Sign up to request clarification or add additional context in comments.

Comments

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.