Selenium Python如何从html表中获取每列的所有行值

时间:2016-09-24 16:55:36

标签: python-2.7 selenium selenium-webdriver

我有一个包含一些行和列的HTMl表。我想打印每行和每列的值。我什么都没打印。当我单步执行代码时,该行没有值:

cols = row.find_elements(By.TAG_NAME, "td")  # Get all the cols

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 = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'search_data_browser_ct_data_browser')))
        rows = table_id.find_elements(By.TAG_NAME, "tr")
        for row in rows:
            # Get the columns
            cols = row.find_elements(By.TAG_NAME, "td")  # Get all the cols
            print "Name col = "
            print cols[1].text # This is the USN column
            print cols[2].text # Match count col
            #print cols[3].text # Source_fields col
    except NoSuchElementException, e:
        print "Element not found "
        print e
        self.save_screenshot("is_results_displayed_in_data_browser")
        return False

当我单步执行代码时,我可以看到行中有值:

rows = table_id.find_elements(By.TAG_NAME, "tr")

如何获取每个单元格,每列的值?

我得到的错误是:

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 109, in is_results_displayed_in_data_browser
    print cols[2].text # Match count col
IndexError: list index out of range

我甚至尝试过:

col = row.find_elements(By.TAG_NAME, "td")[2]  
print col.text

可能是因为第一行有一个标题吗?

我现在正在工作。我不得不迭代列。工作代码是:

    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:
            cols = row.find_elements(By.TAG_NAME, "td")  
            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")

谢谢,问候,Riaz

0 个答案:

没有答案