[英]How to build a dynamic table from python selenium data?
我正在嘗試生成一個動態表,並借助python硒將其發送給該表。
使用WebDriver,我們可以獲取必填字段的動態數據。 但是我無法在單個表中將這些數據制成表格。 我嘗試了循環使用電子郵件功能。
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()
使用上面的代碼,我按照表中的行數收到了電子郵件。 但是我需要將所有這些數據都放在一個表格中,如下面的單個電子郵件中所示
預期產量:
| 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 |
請嘗試以下邏輯。
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.