简体   繁体   English

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

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

I am iterating over a table of rows using Selenium Python. 我正在使用Selenium Python遍历行表。 On the GUI i have deleted an item from a table. 在GUI上,我已从表中删除了一个项目。 In my script I am checking if the item is not there to verify it has been deleted. 在我的脚本中,我正在检查项目是否不存在,以确认它已被删除。 When i iterate over my table, it stops at the first row. 当我遍历我的桌子时,它停在第一行。 It does not continue until to the last row. 直到最后一行才继续。

Eg I am passing in a Name parameter to my method. 例如,我将Name参数传递给我的方法。 I would like to iterate the whole table of rows, column 1 and check the Name is not there. 我想遍历整个表的行,第1列,并检查名称是否不存在。 Return true if Name is not there. 如果Name不存在,则返回true。

My code snippet is: 我的代码段是:

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

My table is stopping at the very first row. 我的桌子停在第一行。 Some help please, Thanks, Riaz 请帮忙,谢谢,里亚兹

I would just use all() : 我只会使用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

Basically, this would return True if all the names don't equal to name which is in other words: return True if name is not there. 基本上,这将返回True如果所有的名字不等于name是换句话说:返回True ,如果name不存在。

As a side note, you are handling NoSuchElementException , but it would never be thrown by any method used inside the try block - find_elements() would return an empty list if no elements matching a locator found. 附带说明一下,您正在处理NoSuchElementException ,但是在try块内使用的任何方法都不会抛出该异常-如果未找到与定位符匹配的元素,则find_elements()将返回一个空列表。

You're telling it to stop on the first row, if that row doesn't contain the text. 您要告诉它在第一行停止(如果该行不包含文本)。

To fix it, change this: 要修复它,请更改此:

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

To this: 对此:

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

You want to look at every row until you find the item you're looking for. 您想查看每一行,直到找到所需的项目。 If you find it, the function should return False. 如果找到它,该函数应返回False。 If you make it all the way through the loop without returning False, then the item you're looking for is not in the table and you should return True. 如果您在循环中一直进行而没有返回False,则您要查找的项目不在表中,您应该返回True。

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

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