【问题标题】:How do i iterate through a table and print out the values of the columns Selenium Webdriver Python我如何遍历表并打印出 Selenium Webdriver Python 列的值
【发布时间】:2015-08-05 15:27:55
【问题描述】:

我在遍历 html 表并从每行的第 2 列输出值时遇到问题。第 1 行中的第 2 列 td[2] 的值为“Name”,第 2 行的第 2 列的值为“Address”,依此类推。 我正在尝试将值打印到控制台。

我的 Python webdriver 代码是:

    # Get the table
table_xpath = self.driver.find_element(By.XPATH, 'html/body/div[2]/div[2]/div/div[4]/div/div[2]/div/div[3]/div/div[5]/div/div[3]/div/div[4]/div/div[2]/div/div[4]/div/div[3]/div/div[2]/div/div/table/tbody')
# Get the rows from the table
rows = table_xpath.find_elements(By.TAG_NAME, "tr.GAT4PNUFG.GAT4PNUMG")
# Get the columns (all the column 2)
cols = rows.find_elements(By.TAG_NAME, "td")[2]
for i in cols:
    print cols.text

我得到的错误是:

  File "C:\Webdriver\ClearCore 501\Pages\data_objects_saved_page.py", line 87, in verify_variables_created
cols = rows.find_elements(By.TAG_NAME, "td")[2]

AttributeError: 'list' 对象没有属性 'find_elements'

HTML sn-p 是:

     <table cellspacing="0" style="table-layout: fixed; width: 100%;">
    <colgroup>
    <tbody>
        <tr class="GAT4PNUFG GAT4PNUMG" __gwt_subrow="0" __gwt_row="0">
            <td class="GAT4PNUEG GAT4PNUGG GAT4PNUHG GAT4PNUNG">
            <td class="GAT4PNUEG GAT4PNUGG GAT4PNUNG">
                <div __gwt_cell="cell-gwt-uid-324" style="outline-style:none;">
                    <span class="linkhover" title="Name" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Name</span>
                </div>
            </td>
            <td class="GAT4PNUEG GAT4PNUGG GAT4PNUNG">
            <td class="GAT4PNUEG GAT4PNUGG GAT4PNUNG">
            <td class="GAT4PNUEG GAT4PNUGG GAT4PNUNG">
            <td class="GAT4PNUEG GAT4PNUGG GAT4PNUBH GAT4PNUNG">
        </tr>
        <tr class="GAT4PNUEH" __gwt_subrow="0" __gwt_row="1">
            <td class="GAT4PNUEG GAT4PNUFH GAT4PNUHG">
            <td class="GAT4PNUEG GAT4PNUFH">
                <div __gwt_cell="cell-gwt-uid-324" style="outline-style:none;">
                    <span class="linkhover" title="Address" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Address</span>
                </div>
            </td>
            <td class="GAT4PNUEG GAT4PNUFH">
            <td class="GAT4PNUEG GAT4PNUFH">
            <td class="GAT4PNUEG GAT4PNUFH">
            <td class="GAT4PNUEG GAT4PNUFH GAT4PNUBH">
        </tr>
        <tr class="GAT4PNUFG" __gwt_subrow="0" __gwt_row="2">
            <td class="GAT4PNUEG GAT4PNUGG GAT4PNUHG">
            <td class="GAT4PNUEG GAT4PNUGG">
                <div __gwt_cell="cell-gwt-uid-324" style="outline-style:none;">
                    <span class="linkhover" title="DOB" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">DOB</span>
                </div>
            </td>
            <td class="GAT4PNUEG GAT4PNUGG">
            <td class="GAT4PNUEG GAT4PNUGG">
            <td class="GAT4PNUEG GAT4PNUGG">
            <td class="GAT4PNUEG GAT4PNUGG GAT4PNUBH">
        </tr>
        <tr class="GAT4PNUEH" __gwt_subrow="0" __gwt_row="3">
            ---
        <tr class="GAT4PNUFG" __gwt_subrow="0" __gwt_row="4">       
            ---
    </tbody>
</table>

我如何遍历这个表并打印出 Python 中的值,请 Webdriver?

【问题讨论】:

    标签: python python-2.7 selenium selenium-webdriver


    【解决方案1】:

    您需要遍历行:

    rows = table_xpath.find_elements(By.TAG_NAME, "tr.GAT4PNUFG.GAT4PNUMG")
    
    for row in rows:
        # Get the columns (all the column 2)
        col = row.find_elements(By.TAG_NAME, "td")[2]
        print col.text
    

    【讨论】:

    • 我在控制台中没有任何输出用于打印 col.text。我已经检查了我的 Xpath 中的表格。在 Firefox 中,它确实找到了表,所以到表的 Xpath 必须没问题。
    • 感谢您提供更新的答案。我仍然没有打印任何内容。我将尝试第二个 TR,因为它具有不同的标签名称。它有
    • 开发人员现在正在将 id 放入代码中。该表将有一个 id,希望它应该可以工作。我不必使用 xpath 来获取表定位器
    • @RiazLadhani 感谢您的更新。如果您稍后需要帮助,请 Lmk。
    • 我现在有了表 ID。 id="data_configuration_feeds_ct_fields_body0"
    【解决方案2】:

    开发人员已将 ID 放入表中。我现在可以工作了。它正在打印第 2 列中的所有单元格值。代码是:

    table_id = self.driver.find_element(By.ID, 'data_configuration_feeds_ct_fields_body0')
    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)        
        col = row.find_elements(By.TAG_NAME, "td")[1] #note: index start from 0, 1 is col 2
        print col.text
    

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签