【问题标题】:Selenium Python iterate over a table of rows it is stopping at the first rowSelenium Python 遍历它在第一行停止的行表
【发布时间】:2016-06-05 12:13:23
【问题描述】:

我正在使用 Selenium Python 遍历一个行表。在 GUI 上,我从表中删除了一个项目。在我的脚本中,我正在检查该项目是否不存在以验证它是否已被删除。 当我遍历我的表时,它停在第一行。它不会持续到最后一行。

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

我的代码 sn-p 是:

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

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

【问题讨论】:

    标签: python python-2.7 selenium selenium-webdriver


    【解决方案1】:

    我只会使用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
    

    基本上,如果所有名称都不等于name,这将返回True,换句话说:如果name 不存在,则返回True

    附带说明,您正在处理NoSuchElementException,但它永远不会被try 块内使用的任何方法抛出 - 如果没有找到与定位器匹配的元素,find_elements() 将返回一个空列表。

    【讨论】:

    • pythonic,但对于刚接触编程的人来说不是很清楚。
    • 我的方法 save_screenshot 是我制作的自定义方法,它在我的基类中。我在 save_screenshot 中插入了一个 Try Catch 块。它应该被称为不应该吗?
    • 如果没有找到匹配的定位器,那么查找元素将返回一个空列表。那么我应该在我的 catch 块中使用什么?
    • @RiazLadhani 啊,找到名字就想截图吧?然后我只需将all() 的结果分配给一个变量,如果它为真,则制作一个屏幕截图。然后返回变量。
    【解决方案2】:

    如果该行不包含文本,您会告诉它在第一行停止。

    要修复它,请更改:

    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。

    【讨论】:

      猜你喜欢
      • 2018-07-26
      • 2020-05-20
      • 1970-01-01
      • 1970-01-01
      • 2020-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多