[英]How to build a dynamic table from python selenium data?
I'm trying to generate a dynamic table and email that table with the help of python selenium. 我正在尝试生成一个动态表,并借助python硒将其发送给该表。
With WebDriver, we were able to get the dynamic data of required fields. 使用WebDriver,我们可以获取必填字段的动态数据。 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 = "xyz@xyz.com"
you = "xyz@xyz.com" # 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 |
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 = "xyz@xyz.com"
you = "xyz@xyz.com" # 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.