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

标签 python python-2.7 selenium selenium-webdriver

我正在使用 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

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

最佳答案

我只会使用 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,换句话说:返回 True 如果 name 不存在。

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

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

相关文章:

python - SQLAlchemy 删除级联完整性错误

python - 更新 Pyinstaller 的 .Spec 文件被重置

python - 如何获取与子字符串匹配的文件夹名称?

selenium - 如何通过传递复选框名称来选择复选框 - 使用 Selenium/Webdriver

java - java中的 Selenium 异常(org.openqa.selenium.remote.UnreachableBrowserException)

java - 如何使用Java减去两个系统时间

python - scrapy : how to test the delay between every requests

python - 两个数据帧的索引和列的并集

python-2.7 - 如何在Python中从csv文件创建一个词袋?

python - 如何使只读属性可变?