【问题标题】:How to build a dynamic table from python selenium data?如何从 python selenium 数据构建动态表?
【发布时间】:2019-12-01 08:46:51
【问题描述】:

我正在尝试在 python selenium 的帮助下生成一个动态表并通过电子邮件发送该表。

借助 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   |

【问题讨论】:

标签: python selenium


【解决方案1】:

试试下面的逻辑。

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    相关资源
    最近更新 更多