简体   繁体   中英

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. On the GUI i have deleted an item from a table. 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. I would like to iterate the whole table of rows, column 1 and check the Name is not there. Return true if Name is not there.

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() :

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.

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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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