繁体   English   中英

Selenium Python遍历在第一行停止的行表

[英]Selenium Python iterate over a table of rows it is stopping at the first row

我正在使用Selenium Python遍历行表。 在GUI上,我已从表中删除了一个项目。 在我的脚本中,我正在检查项目是否不存在,以确认它已被删除。 当我遍历我的桌子时,它停在第一行。 直到最后一行才继续。

例如,我将Name参数传递给我的方法。 我想遍历整个表的行,第1列,并检查名称是否不存在。 如果Name不存在,则返回true。

我的代码段是:

def is_data_objet_deleted(self, name):
    # Params : name : the name of the data object, e.g. Name, Address, Phone, DOB
     try:
        #WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'data_configuration_data_objects_ct_fields_body')))
        table_id = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'data_configuration_data_objects_ct_fields_body')))
        rows = table_id.find_elements(By.TAG_NAME, "tr")
        for row in rows:
            # Get the columns
            col_name = row.find_elements(By.TAG_NAME, "td")[1]  # This is the Checkbox column
            col_name = row.find_elements(By.TAG_NAME, "td")[2]  # This is the Name column
            print "col_name.text = "
            print col_name.text
            if (col_name.text != name):
                return True
            #return False
     except NoSuchElementException, e:
        print "Element not found "
        print e
        self.save_screenshot("data_objects_page_saved_details")
        return False

我的桌子停在第一行。 请帮忙,谢谢,里亚兹

我只会使用all()

def is_data_objet_deleted(self, name):
    table = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'data_configuration_data_objects_ct_fields_body')))
    rows = table_id.find_elements(By.TAG_NAME, "tr")

    result = all(row.find_elements(By.TAG_NAME, "td")[2].text != name
                 for row in rows)

    # save a screenshot if there was name found
    if not result:
        self.save_screenshot("data_objects_page_saved_details")
    return result

基本上,这将返回True如果所有的名字不等于name是换句话说:返回True ,如果name不存在。

附带说明一下,您正在处理NoSuchElementException ,但是在try块内使用的任何方法都不会抛出该异常-如果未找到与定位符匹配的元素,则find_elements()将返回一个空列表。

您要告诉它在第一行停止(如果该行不包含文本)。

要修复它,请更改此:

for row in rows:
    ...
    if (col_name.text != name):
        return True

对此:

for row in rows:
    ...
    if (col_name.text == name):
        return False
return True

您想查看每一行,直到找到所需的项目。 如果找到它,该函数应返回False。 如果您在循环中一直进行而没有返回False,则您要查找的项目不在表中,您应该返回True。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM