我正在使用Selenium Python迭代一个行表。在GUI上,我从表中删除了一个项目。在我的脚本中,我检查项目是否不存在,以验证它是否已被删除。
当我遍历表时,它停在第一行。直到最后一行才继续。
我正在向我的方法传递一个Name参数。我想遍历整个行表,列1,并检查名称是否不存在。如果名称不存在,则返回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()如果找不到与定位器匹配的元素,则将返回空列表。

关于python - Selenium Python遍历在第一行停止的行表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35581254/

10-09 22:48
查看更多