2016-11-04 61 views
0

我使用Python中BeuatifulSoup提取從報告測試用例的數量的總數和我得到的錯誤:BeautifulSoup AttributeError的:NoneType對象沒有屬性發現

AttributeError: 'NoneType' object has no attribute 'find' 

完整的錯誤跟蹤:

Traceback (most recent call last): 
    File "C:/test_runners 2 edit project/selenium_regression_test_5_1_1/Email/email_selenium_report_for_edit_project_test.py", line 32, in <module> 
    report_for_edit_project_test.send_report_summary_from_htmltestrunner_selenium_grouping_edit_project_report("IE10") 
    File "C:\test_runners 2 edit project\selenium_regression_test_5_1_1\Email\report_for_edit_project_test.py", line 567, in send_report_summary_from_htmltestrunner_selenium_grouping_edit_project_report 
    text.append(extract_total_from_grouping_report_htmltestrunner(browser_version)) 
    File "C:\test_runners 2 edit project\selenium_regression_test_5_1_1\Email\report_for_edit_project_test.py", line 132, in extract_total_from_grouping_report_htmltestrunner 
    tr_total_row.find('td') 

我的方法實現:

def extract_total_from_grouping_report_htmltestrunner(
    browser_version): # Extract the total count of the number of test cases from the report e.g 93 test cases 
# Params browser_version : e.g. IE11, IE10 
# filename = (
# r"E:\test_runners 2 edit project\selenium_regression_test_5_1_1\TestReport\ClearCore501_Automated_GUI_Regression_EditProject_Grouping_" + browser_version + "_TestReport.html") 
if browser_version == "IE11": 
    filename = Globals.test_report_for_grouping_edit_project_filepath_IE11 
elif browser_version == "IE10": 
    filename = Globals.test_report_for_grouping_edit_project_filepath_IE10 
html_report_part = open(filename, 'r') 
soup = BeautifulSoup(html_report_part, "html.parser") 
tr_total_row = soup.find('tr', {'id': 'total_row'}) 
# tr_total = tr_total_row.find('strong', text='Status:').parent 
# tr_total_row.find(text=True, recursive=False) 
tr_total_row.find('td') 
col = tr_total_row.find_all('td') 
total_column_0 = col[0].string.strip() 
count_column_1 = col[1].string.strip() 

# print tr_total_row.text 
# print total_column_0 
# print count_column_1 
total_testcase_count = total_column_0.join(" ").join(count_column_1) 

total_items = [] 
total_items.append(total_column_0) 
total_items.append("") 
total_items.append(" TestCases = ") 
total_items.append(count_column_1) 
str = "".join(total_items) 
# str = "\n".join(total_items) 
print str 

# return tr_total_row.text 
# return total_testcase_count 
return str 

我不知道爲什麼它是抱怨,錯誤的是上tr_total_row.find行( 'TD')

從報告的HTML樣本:

<table id='result_table'> 
    tr id='total_row'> 
    <td>Total</td> 
    <td>67</td> 
    <td>66</td> 
    <td>0</td> 
    <td>1</td> 
    <td>&nbsp;</td> 
</tr> 
</table> 

一些幫助,請。謝謝,問候,Riaz

回答

1

HTML語法無效。當我嘗試你的html並且打印soup時,語法錯誤在我主演。

>>> soup 
<body>\n<table id="result_table">\n tr id='total_row'&gt;\n <td>Total</td>\n<td>67</td>\n<td>66</td>\n<td>0</td>\n<td>1</td>\n<td>\xa0</td>\n</table></body>\n\n\n 

TR ID = 'total_row'>

固定的語法後:

>>> soup 
<body>\n<table id="result_table">\n<tr id="total_row">\n<td>Total</td>\n<td>67</td>\n<td>66</td>\n<td>0</td>\n<td>1</td>\n<td>\xa0</td>\n</tr>\n</table>\n</body>\n 

腳本返回:

Total TestCases = 67

+0

大,感謝您的幫助。 –

相關問題