BeautifulSoup AttributeError:NoneType对象没有属性find

时间:2016-11-04 17:22:26

标签: python-2.7 beautifulsoup

我在Python中使用BeuatifulSoup从报告中提取测试用例数的总数,我收到错误:

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 个答案:

答案 0 :(得分: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 =&#39; total_row&#39;&gt;

修复语法后:

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