簡體   English   中英

Selenium Python如何從元素表中找到指定的行和列

[英]Selenium Python how to locate a specified row and a column from a table of elements

我的桌子上有一些行和列。 我想遍歷該表並找到第81和82行以及列索引4(Surname列),我想檢查第4列中第81和82行的值。數據是固定的,因此為我的行標識了第81和82行測試目的還可以。

我已經開始使用一些代碼來獲取表並遍歷行。 如何直接轉到第81行和第4列?

我的代碼段是:

def is_surname_Campbell_and_CAMPBELL_together(self): # is the surname "Campbell" and "CAMPBELL" together
    try:
        table_id = WebDriverWait(self.driver, 20).until(
            EC.presence_of_element_located((By.ID, 'data_configuration_view_preview_dg_main_body')))
        rows = table_id.find_elements(By.TAG_NAME, "tr")
        for row in rows:
            # Get the columns
            col_sname = row.find_elements(By.TAG_NAME, "td")[4]  # This is the SNAME column
            print "col_sname.text = "
            if col_sname.text == "Campbell":
                return True
        return False
    except NoSuchElementException, e:
        print "Element not found "
        print e
        self.save_screenshot("is_surname_Campbell_and_CAMPBELL_together")
        return False

HTML片段(一小部分,否則粘貼將太長)

    <table id="data_configuration_view_preview_dg_main_body" cellspacing="0" style="table-layout: fixed; width: 100%; margin-bottom: 17px;">
<colgroup>
<tbody>
    <tr class="GJPPK2LBJM GJPPK2LBAN" __gwt_subrow="0" __gwt_row="0">
    <td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBLM GJPPK2LBBN">
    <td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
        <div __gwt_cell="cell-gwt-uid-756" style="outline-style:none;" tabindex="0">
            <span class="" title="f1/48" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">f1/48</span>
        </div>
    </td>
    <td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
        <div __gwt_cell="cell-gwt-uid-757" style="outline-style:none;">
            <span class="" title="" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;"/>
        </div>
    </td>
    <td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
        <div __gwt_cell="cell-gwt-uid-758" style="outline-style:none;">
            <span class="" title="Keith" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Keith</span>
        </div>
    </td>
    <td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
        <div __gwt_cell="cell-gwt-uid-759" style="outline-style:none;">
            <span class="" title="Campbell" style="background-color:yellow;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Campbell</span>
        </div>
    </td>
    <td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
        <div __gwt_cell="cell-gwt-uid-760" style="outline-style:none;">
            <span class="" title="" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;"/>
        </div>
    </td>
<td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
<td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
<td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
</tbody>
</table>

謝謝,里亞茲

通常情況下,第81行第4列中的單元格值可以定義為

driver.find_element_by_xpath('//tr[81]/td[4]').text

PS。 HTML元素索引是從[1]元素開始的,而不是像Python那樣從[0]開始的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM