【发布时间】:2018-06-27 11:00:55
【问题描述】:
我有一个包含一些行和列的 HTML 表格。我可以遍历行并打印出所有列值。 我想打印第 3 列中的值。我该怎么做?
HTML sn-p 是:
<table id="search_data_browser_ct_data_browser" class="GFNQNVHJE" cellspacing="0" __gwtcellbasedwidgetimpldispatchingfocus="true" __gwtcellbasedwidgetimpldispatchingblur="true">
<thead aria-hidden="false">
<colgroup>
<tbody>
<tr class="GFNQNVHCD GFNQNVHJD" __gwt_subrow="0" __gwt_row="0">
<td class="GFNQNVHBD GFNQNVHDD GFNQNVHED GFNQNVHKD">
<div __gwt_cell="cell-gwt-uid-193" style="outline-style:none;">
<span class="linkhover" title="31" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">31</span>
</div>
</td>
<td class="GFNQNVHBD GFNQNVHDD GFNQNVHKD">
<div __gwt_cell="cell-gwt-uid-194" style="outline-style:none;">1</div>
</td>
<td class="GFNQNVHBD GFNQNVHDD GFNQNVHKD">
<div __gwt_cell="cell-gwt-uid-195" style="outline-style:none;">
<span class="linkhover" title="Mr|Batman|Bane|Male" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Mr|Batman|Bane|Male</span>
</div>
</td>
<td class="GFNQNVHBD GFNQNVHDD GFNQNVHKD">
<td class="GFNQNVHBD GFNQNVHDD GFNQNVHKD">
<td class="GFNQNVHBD GFNQNVHDD GFNQNVHOD GFNQNVHKD">
</tr>
<tr class="GFNQNVHCE" __gwt_subrow="0" __gwt_row="1">
<tr class="GFNQNVHCD" __gwt_subrow="0" __gwt_row="2">
<tr class="GFNQNVHCE" __gwt_subrow="0" __gwt_row="3">
<tr class="GFNQNVHCD" __gwt_subrow="0" __gwt_row="4">
<tr class="GFNQNVHCE" __gwt_subrow="0" __gwt_row="5">
<tr class="GFNQNVHCD" __gwt_subrow="0" __gwt_row="6">
<tr class="GFNQNVHCE" __gwt_subrow="0" __gwt_row="7">
<tr class="GFNQNVHCD" __gwt_subrow="0" __gwt_row="8">
<tr class="GFNQNVHCE" __gwt_subrow="0" __gwt_row="9">
</tbody>
<tbody style="display: none;">
<tfoot style="display: none;" aria-hidden="true"/>
</table>
我的方法实现是:
def is_results_displayed_in_data_browser(self):
try:
table_id = self.driver.find_element(By.ID, 'search_data_browser_ct_data_browser')
rows = table_id.find_elements(By.TAG_NAME, "tr") # get all of the rows in the table
for row in rows:
# Get the columns (all the column 2)
cols = row.find_elements(By.TAG_NAME, "td") # note: index start from 0, 1 is col 2
for col in cols:
print col.text
except NoSuchElementException, e:
print "Element not found "
print e
self.save_screenshot("is_results_displayed_in_data_browser")
我试过了:
print col[2].text
我得到的错误是:
Traceback (most recent call last):
File "E:\test_runners 2 edit project\selenium_regression_test_5_1_1\LADEMO_Matching_and_Reporting_TestCase\Lademo_Matching_and_Reporting_TestCase.py", line 496, in test_000008_simple_text_search
data_browser_page.is_results_displayed_in_data_browser()
File "E:\test_runners 2 edit project\selenium_regression_test_5_1_1\Pages\Reports\reports_data_browser.py", line 104, in is_results_displayed_in_data_browser
print col[2].text
TypeError: 'WebElement' object does not support indexing
我也试过了:
for col in cols:
print cols[2].text
我得到错误:
Traceback (most recent call last):
File "E:\test_runners 2 edit project\selenium_regression_test_5_1_1\Base\BaseTestCase.py", line 174, in tearDownClass
cls.login_page.click_logout()
File "E:\test_runners 2 edit project\selenium_regression_test_5_1_1\Pages\login.py", line 129, in click_logout
self.click_yes_from_confirm_dialog_to_confirm()
File "E:\test_runners 2 edit project\selenium_regression_test_5_1_1\Pages\base.py", line 106, in click_yes_from_confirm_dialog_to_confirm
yes_button_element = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.ID, 'message_dialog_b_yes')))
File "E:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
TimeoutException: Message:
谢谢,里亚兹
【问题讨论】:
-
是否出现错误?
-
您的错误在此代码块中,而不是您上面提到的代码中。
yes_button_element = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.ID, 'message_dialog_b_yes'))) -
我认为你应该做"print cols[2].text"
-
如果我打印 cols[2].text 我得到 IndexError: list index out of range
-
如果我打印 cols[0].text 它会打印出第一列中的所有 ID 值。奇怪的 cols[2] 不起作用
标签: python-2.7 selenium selenium-webdriver