Selenium Python遍历在第一行停止的行表 [英] Selenium Python iterate over a table of rows it is stopping at the first row
问题描述
我正在使用Selenium Python遍历行表.在GUI上,我已从表中删除了一个项目.在我的脚本中,我正在检查该项目是否不存在,以确认它已被删除. 当我遍历我的桌子时,它停在第一行.直到最后一行才继续.
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.
例如我将Name参数传递给我的方法.我想遍历整个表的行,第1列,并检查名称是否不存在.如果名称"不存在,则返回true.
E.g. 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.
我的代码段是:
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
基本上,如果所有名称均不等于name
,则返回True
;换句话说:如果name
不存在,则返回True
.
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.
作为旁注,您正在处理NoSuchElementException
,但是它永远不会被try
块内使用的任何方法抛出-如果找不到与定位符匹配的元素,find_elements()
将返回一个空列表.
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.
这篇关于Selenium Python遍历在第一行停止的行表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!