我有一个包含一些行和列的HTML表。我可以遍历行并打印出所有列值。 我想打印第3列中的值。我该怎么做?
HTML代码段为:
<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:
谢谢Riaz
答案 0 :(得分:2)
在这里,我将为您提供工作代码,其中提取所有td标记的值。
void Process::FrameImageReady(cv::Mat FrameImage)
{
if (modedebug)
cv::imshow("bgr", FrameImage);
if (cv::waitKey(1)==112){
cam->setButtonPause(!(getButtonPause()));
}
}
答案 1 :(得分:2)
'WebElement'对象不支持索引
实际上col
是单WebElement
,而你在列表中,所以它应该是
print cols[2].text
如果我打印cols [2] .text我得到IndexError:list index超出范围
在传递索引之前,您需要检查cols
列表长度以克服此错误,如下所示: -
if len(cols) >= 3 :
print cols[2].text
答案 2 :(得分:0)
我试过了:
print col[2].text
试试这个
if len(cols)>2:
cols[2].text
答案 3 :(得分:0)
您可以使用CSS选择器在td下查找节点。使用td:nth-child()语法。
table_id = self.driver.find_element(By.ID,'tableid')
rows = table_id.find_elements(By.TAG_NAME, "tr")
for row in rows:
cols = row.find_elements_by_css_selector('td:nth-child(2)')
for col in cols:
print('this is {}'.format(col.text))