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