Selenium Python如何从元素表中定位指定的行和列

2024-04-28 12:55:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一张有一些行和列的表。我想遍历表并找到第81行和第82行以及列索引4(姓氏列) 我想检查第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片段(一个小部分,否则将太长而无法粘贴)

^{pr2}$

谢谢, 里亚兹


Tags: andthe代码selfbyreturnistable
1条回答
网友
1楼 · 发布于 2024-04-28 12:55:20

一般情况下,81行第4列的单元格值可以定义为

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

PS.HTML元素索引从[1]元素开始,但不是从[0]开始,如Python

相关问题 更多 >