【发布时间】:2016-09-09 14:47:45
【问题描述】:
我有一个来自 Nose 的 html 格式的测试报告文件。 我想在 Python 中提取文本的某些部分。我将在消息部分的电子邮件中发送此内容。
我有以下示例:
<!DOCTYPE html>
<html>
<head>
<title>Unit Test Report</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style>
body {
font-family: Calibri, "Trebuchet MS", sans-serif;
}
* {
word-break: break-all;
}
table, td, th, .dataid {
border: 1px solid #aaa;
border-collapse: collapse;
background: #fff;
}
section {
background: rgba(0, 0, 0, 0.05);
margin: 2ex;
padding: 1ex;
border: 1px solid #999;
border-radius: 5px;
}
h1 {
font-size: 130%;
}
h2 {
font-size: 120%;
}
h3 {
font-size: 100%;
}
h4 {
font-size: 85%;
}
h1, h2, h3, h4, a[href] {
cursor: pointer;
color: #0074d9;
text-decoration: none;
}
h3 strong, a.failed {
color: #ff4136;
}
.failed {
color: #ff4136;
}
a.success {
color: #3d9970;
}
pre {
font-family: 'Consolas', 'Deja Vu Sans Mono',
'Bitstream Vera Sans Mono', 'Monaco',
'Courier New', monospace;
}
.test-details,
.traceback {
display: none;
}
section:target .test-details {
display: block;
}
</style>
</head>
<body>
<h1>Overview</h1>
<section>
<table>
<tr>
<th>Class</th>
<th class="failed">Fail</th>
<th class="failed">Error</th>
<th>Skip</th>
<th>Success</th>
<th>Total</th>
</tr>
<tr>
<td>Regression_TestCase.RegressionProject_TestCase2.RegressionProject_TestCase2</td>
<td class="failed">1</td>
<td class="failed">9</td>
<td>0</td>
<td>219</td>
<td>229</td>
</tr>
<tr>
<td><strong>Total</strong></td>
<td class="failed">1</td>
<td class="failed">9</td>
<td>0</td>
<td>219</td>
<td>229</td>
</tr>
</table>
</section>
<h1>Failure details</h1>
<section>
<h2>Regression_TestCase.RegressionProject_TestCase2.RegressionProject_TestCase2 (1 failures, 9 errors)</h2>
<div>
<section id="Regression_TestCase.RegressionProject_TestCase2.RegressionProject_TestCase2:test_00010_import_user_invalid_credentials">
<h3>test_00010_import_user_invalid_credentials: <strong>selenium.common.exceptions.NoSuchElementException</strong></h3>
<div class="test-details">
<h4>Traceback</h4>
<pre class="traceback">Traceback (most recent call last):
File "C:\Python27\lib\unittest\case.py", line 329, in run
testMethod()
File "C:\test_runners\selenium_regression_test_5_1_1\ClearCore - Regression Test\Regression_TestCase\RegressionProject_TestCase2.py", line 221, in test_00010_import_user_invalid_credentials
Globals.login_password_invalid)
File "C:\test_runners\selenium_regression_test_5_1_1\ClearCore - Regression Test\Pages\security.py", line 51, in enter_invalid_userid_and_password
self.enter_user_id(userid)
File "C:\test_runners\selenium_regression_test_5_1_1\ClearCore - Regression Test\Pages\security.py", line 32, in enter_user_id
user_id_element = self.get_element(*MainPageLocators.security_user_id_textfield_xpath)
File "C:\test_runners\selenium_regression_test_5_1_1\ClearCore - Regression Test\Pages\base.py", line 40, in get_element
element = self.driver.find_element(by=how, value=what)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 712, in find_element
{'using': by, 'value': value})['value']
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: Message: Unable to find element with xpath == //span[@class="gwt-InlineLabel marginbelow myinlineblock" and contains(text(), "User ID (including domain)")]/following-sibling::input
-------------------- >> begin captured stdout << ---------------------
*** Test import_invalid_user_credentials ***
05_12_1616_49_42
//span[@class="gwt-InlineLabel marginbelow myinlineblock" and contains(text(), "User ID (including domain)")]/following-sibling::input
Element not found
Message: Unable to find element with xpath == //span[@class="gwt-InlineLabel marginbelow myinlineblock" and contains(text(), "User ID (including domain)")]/following-sibling::input
05_12_1616_51_54
--------------------- >> end captured stdout << ----------------------
----
# There is more html below. I have not included everything. It will be too long otherwise.
如果我在浏览器中打开文件,格式如下所示: 这是我想从 html 文件中提取的文本。
Class Fail Error Skip Success Total
Regression_TestCase 1 9 0 219 229
请问我该怎么做?将其保留为表格格式会很好。 谢谢,里亚兹
【问题讨论】:
-
您是否尝试过使用 xml 解析库? (如docs.python.org/2.7/library/…)
-
您希望输出采用什么格式?您希望它看起来像 excel 中的表格(例如 csv),还是想要一个包含这些行、列和间距的文本文件?
标签: python-2.7