【问题标题】:Selecting table row headers with xpath使用 xpath 选择表格行标题
【发布时间】:2015-12-21 11:27:16
【问题描述】:

我正在选择表格标题,但需要使用类似的方法来为我的行标题中的项目返回索引。

这是我的尝试...

def return_table_index():
        table = driver.find_element_by_css_selector('#main > table:nth-child(6)')

第 1 部分列索引(为“table_header”正确返回 int)

        table_headers = table.find_elements_by_css_selector('tbody > tr:nth-child(1) > th')
        index_of_column = None
        for i in range(len(table_headers)):
            if table_headers[i].text == table_header:
                index_of_column = i + 1
        print index_of_column

第 2 部分行索引(不返回,我想要一个 int 表示 'row_header')

        row_headers = table.find_elements_by_css_selector('tbody > tr:nth-child(1) > th')
        index_of_row = None
        for i in range(len(row_headers)):
            if row_headers[i].text == row_header:
                index_of_row = i + 1
        print index_of_row

如何更新第 2 部分中的 xpath 以返回 3 的行索引,即 4。

Here is my sample data

【问题讨论】:

    标签: python python-2.7 selenium xpath


    【解决方案1】:

    我认为您基本上需要在每一行中获取第一个td

    tbody > tr > td:nth-child(1)
    

    【讨论】:

    • 谢谢:) 但这仍然对我产生“无”,打印出“index_of_row”。我是否必须寻找转换为 str 的“3”?编辑...我愿意!它奏效了:)
    • 我不得不添加 +2 而不是 +1 来说明标题行(我认为)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多